Issue installing node-postgres with npm (on windows)

Problem

I am trying to install pg on windows using npm, but receive the following error:

c:\Users\someone\Dropbox\somefolder>npm install -g pg
npm http GET https://registry.npmjs.org/pg
npm http 304 https://registry.npmjs.org/pg
npm http GET https://registry.npmjs.org/generic-pool/2.0.2
npm http 304 https://registry.npmjs.org/generic-pool/2.0.2

> pg@0.11.3 install C:\Users\someone\AppData\Roaming\npm\node_modules\pg
> node-gyp rebuild || (exit 0)


C:\Users\someone\AppData\Roaming\npm\node_modules\pg>node "C:\Program Files (x86)\nodejs\node_modules\npm\bin\node-gyp-
yp\bin\node-gyp.js" rebuild
'pg_config' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\gyp", line 18, in <module>
    sys.exit(gyp.main(sys.argv[1:]))
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\__init__.py", line 511, in main
    return gyp_main(args)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\__init__.py", line 494, in gyp_main
    options.circular_check)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\__init__.py", line 133, in Load
    depth, generator_input_info, check, circular_check)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 2378, in Load
    depth, check)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 384, in LoadTargetBuildFile
    build_file_data, PHASE_EARLY, variables, build_file_path)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1053, in ProcessVariablesAndConditionsInD
    build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1068, in ProcessVariablesAndConditionsInL
    ProcessVariablesAndConditionsInDict(item, phase, variables, build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1027, in ProcessVariablesAndConditionsInD
    ProcessConditionsInDict(the_dict, phase, variables, build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 904, in ProcessConditionsInDict
    variables, build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1046, in ProcessVariablesAndConditionsInD
    build_file, key)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1046, in ProcessVariablesAndConditionsInD
    build_file, key)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1053, in ProcessVariablesAndConditionsInD
    build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 1072, in ProcessVariablesAndConditionsInL
    expanded = ExpandVariables(item, phase, variables, build_file)
  File "C:\Users\someone\.node-gyp\0.8.16\tools\gyp\pylib\gyp\input.py", line 714, in ExpandVariables
    (contents, p.returncode))
Exception: Call to 'pg_config --libdir' returned exit status 1. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\node-gyp\lib\co
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:99:17)
gyp ERR! stack     at Process._handle.onexit (child_process.js:678:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "
gyp ERR! cwd C:\Users\someone\AppData\Roaming\npm\node_modules\pg
gyp ERR! node -v v0.8.16
gyp ERR! node-gyp -v v0.7.3
gyp ERR! not ok
pg@0.11.3 C:\Users\someone\AppData\Roaming\npm\node_modules\pg
└── generic-pool@2.0.2

My versions are as follow:

c:\>node -v
v0.8.16

c:\>npm -v
1.1.69

When I check out the repository and try node-gyp rebuild I get the following error message:

C:\Users\someone\Downloads\node-postgres.git>node-gyp rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@0.8.3
gyp info using node@0.8.16 | win32 | ia32
gyp info spawn python
gyp info spawn args [ 'C:\\Users\\lamassey\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\gyp\\gyp',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=auto',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\lamassey\\Downloads\\node-postgres.git\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\lamassey\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\lamassey\\.node-gyp\\0.8.16\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\lamassey\\.node-gyp\\0.8.16',
gyp info spawn args   '-Dmodule_root_dir=C:\\Users\\lamassey\\Downloads\\node-postgres.git',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--generator-output',
gyp info spawn args   'C:\\Users\\lamassey\\Downloads\\node-postgres.git\\build',
gyp info spawn args   '-Goutput_dir=.' ]
'pg_config' is not recognized as an internal or external command,
operable program or batch file.
gyp: Call to 'pg_config --libdir' returned exit status 1. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (C:\Users\lamassey\AppData\Roaming\npm\node_modules\node-gyp\lib\configure.js:420:16)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:99:17)
gyp ERR! stack     at Process._handle.onexit (child_process.js:678:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Users\\lamassey\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\lamassey\Downloads\node-postgres.git
gyp ERR! node -v v0.8.16
gyp ERR! node-gyp -v v0.8.3
gyp ERR! not ok
Problem courtesy of: i3enhamin

Solution

Per the line

'pg_config' is not recognized as an internal or external command

I just need to install PostgreSQL before trying to

npm install pg
Solution courtesy of: i3enhamin

Discussion

On OSx see Peter Lyons answer (accepted). The Xcode Version option worked for me. https://stackoverflow.com/a/12825911/296765

Discussion courtesy of: Kreegr

Have you checked out the installation instructions for node-gyp?

https://github.com/TooTallNate/node-gyp#installation

I recently had issues with node-gyp on Windows also while trying to install a separate module that depended on it. Make sure you have each of the items installed that it requires. Pay attention to this part specifically:

For 64-bit builds of node and native modules you will also need the Windows 7 64-bit SDK If the install fails, try uninstalling any C++ 2010 x64&x86 Redistributable that you have installed first.

Discussion courtesy of: jholloman

This recipe can be found in it's original form on Stack Over Flow.