npm installation failure for PostgreSQL driver on Windows

I’m facing problems when trying to install the PostgreSQL database driver using npm on my Windows machine. The process keeps failing because of errors related to Python, even though I have added Python to my environment variables.

I’ve experimented with various ways to set the Python path, but with no luck. Here are the different formats I used:

  • C:/Program Files/Python
  • "C:/Program Files/Python"
  • C:/Program Files/Python/python.exe
  • "C:/Program Files/Python/python.exe"

This is the error message I keep encountering:

D:\Projects\GitHub\inmotion>npm -g install pg
npm http GET https://registry.npmjs.org/pg
npm http 200 https://registry.npmjs.org/pg
npm http GET https://registry.npmjs.org/generic-pool/1.0.12
npm http 200 https://registry.npmjs.org/generic-pool/1.0.12
npm http GET https://registry.npmjs.org/generic-pool/-/generic-pool-1.0.12.tgz
npm http 200 https://registry.npmjs.org/generic-pool/-/generic-pool-1.0.12.tgz

> [email protected] install C:\Users\Guzza\AppData\Roaming\npm\node_modules\pg
> node-gyp rebuild || (exit 0)

gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:/Program Files/Python", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:95:14)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Guzza\AppData\Roaming\npm\node_modules\pg
gyp ERR! node -v v0.8.4
gyp ERR! not ok

Has anyone faced a similar issue? What is the proper way to set up Python for npm installations on Windows?

i had the same problem too! try installing windows-build-tools with npm, it automatically sets up python and visual studio. way less hassle than doing it all manually.

First, make sure Python’s actually installed. Grab Python 2.7 from the official site and check “Add Python to PATH” when installing. The error means node-gyp can’t find Python at all. Once you’ve installed Python 2.7, restart your command prompt and manually set the environment variable: npm config set python "C:\Python27\python.exe" (change the path if needed). Your Node.js v0.8.4 is pretty old though - it doesn’t play nice with newer build tools. I’d upgrade to a recent Node.js version instead. It has better Windows support and you probably won’t need to mess with Python config at all.

node-gyp needs the actual python.exe file, not just the directory. Point your PYTHON environment variable to something like C:\Program Files\Python\python.exe (use double backslashes for Windows paths). Also, you’re probably using the wrong Python version - node-gyp needs Python 2.7 for old Node.js versions like yours (v0.8.4). Honestly, I’d just upgrade Node.js instead. Newer versions handle native module compilation way better on Windows.