Wang Tim
Wang Tim

Reputation: 127

Node-gyp version is different when using npm install

When I run

npm install -g ffi -verbose

it raises error, and log suggests node-gyp version is 8.2.0:

...
npm ERR! D:\Programs\NodeJS\node_global_modules\node_modules\ffi\node_modules\ref\src\binding.cc(644,8): warning C4996: 'Nan::ForceSet': ������Ϊ�ѷ�� [D:\Programs\NodeJS\node_global_modules\node_modules\ffi\node_modules\ref\build\binding.vcxproj]
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp info find Python using Python version 3.8.8 found at "D:\Programs\Anaconda3\python.exe"
npm ERR! gyp info find VS using VS2019 (16.10.31515.178) found at:
npm ERR! gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
npm ERR! gyp info find VS run with --verbose for detailed information
npm ERR! gyp info spawn D:\Programs\Anaconda3\python.exe
...

This is different from what I see when running node-gyp -v:

C:\Users\Administrator>node-gyp -v
v8.4.1

and could possibly explain why it uses python 3.8 and MSVC 2019 even though I have already run:

npm config set python python2.7
npm config set msvs_version 2015

I don't know why there is another node-gyp on my PC, and how to use the proper version.

Upvotes: 5

Views: 10237

Answers (3)

Luca Fagioli
Luca Fagioli

Reputation: 13359

This happens because npm bundles its own, internal, copy of node-gyp. This internal copy is independent of any globally installed copy of node-gyp that may have been installed via npm install -g node-gyp.

This means that while node-gyp doesn't get installed into your $PATH by default, npm still keeps its own copy to invoke when you attempt to npm install a native add-on.

Sometimes, you may need to update npm's internal node-gyp to a newer version than what is installed. A simple npm install -g node-gyp won't do the trick since npm will still continue to use its internal copy over the global one.

So instead:

Version of npm

We need to start by knowing your version of npm:

npm --version

Linux, macOS, Solaris, etc.

Unix is easy. Just run the following command.

If your npm is version 7 or 8, do:

$ npm explore npm/node_modules/@npmcli/run-script -g -- npm_config_global=false npm install node-gyp@latest

Else if your npm is version less than 7, do:

$ npm explore npm/node_modules/npm-lifecycle -g -- npm install node-gyp@latest

If the command fails with a permissions error, please try sudo and then the command.

Windows

Windows is a bit trickier, since npm might be installed to the "Program Files" directory, which needs admin privileges in order to modify on current Windows. Therefore, run the following commands inside a cmd.exe started with "Run as Administrator":

First we need to find the location of node. If you don't already know the location that node.exe got installed to, then run:

$ where node

Now cd to the directory that node.exe is contained in e.g.:

$ cd "C:\Program Files\nodejs"

If your npm version is 7 or 8, do:

cd node_modules\npm\node_modules\@npmcli\run-script

Else if your npm version is less than 7, do:

cd node_modules\npm\node_modules\npm-lifecycle

Finish by running:

$ npm install node-gyp@latest

Source: https://github.com/nodejs/node-gyp/blob/main/docs/Force-npm-to-use-global-node-gyp.md

Upvotes: 5

RamlahAziz
RamlahAziz

Reputation: 75

So interesting find, I thought I'd share it here. I was also facing a similar situation where my global node-gyp was 9.0.0 and I had updated the npm node-gyp too like mentioned here . I was using

  • node 16.2.0
  • npm 7.15.1
  • node-gyp 9.0.0

When I ran npm install @abandonware/bleno I got the following error and thought it was npm using the wrong node-gyp version.

416 error code 1
417 error path D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
418 error command failed
419 error command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
420 error gyp info it worked if it ends with ok
420 error gyp info using [email protected]
420 error gyp info using [email protected] | win32 | x64
420 error gyp ERR! configure error
420 error gyp ERR! stack Error: Command failed: C:\Python39\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
420 error gyp ERR! stack   File "<string>", line 1
420 error gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
420 error gyp ERR! stack                       ^
420 error gyp ERR! stack SyntaxError: invalid syntax
420 error gyp ERR! stack
420 error gyp ERR! stack     at ChildProcess.exithandler (node:child_process:326:12)
420 error gyp ERR! stack     at ChildProcess.emit (node:events:365:28)
420 error gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
420 error gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
420 error gyp ERR! System Windows_NT 10.0.19044
420 error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\VSCode Projects\\react-native-ble-read-peripheral\\food-bank-peripheral\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
420 error gyp ERR! cwd D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
420 error gyp ERR! node -v v16.2.0
420 error gyp ERR! node-gyp -v v3.8.0
420 error gyp ERR! not ok
421 verbose exit 1

But then I saw this comment here and here so I went back to look at the log file and saw that the actual error started above here (line 409)

409 verbose stack Error: command failed
409 verbose stack     at ChildProcess.<anonymous> (C:\Users\Ramlah\AppData\Roaming\nvm\v16.2.0\node_modules\npm\node_modules\@npmcli\run-script\node_modules\@npmcli\promise-spawn\index.js:64:27)
409 verbose stack     at ChildProcess.emit (node:events:365:28)
409 verbose stack     at maybeClose (node:internal/child_process:1067:16)
409 verbose stack     at Socket.<anonymous> (node:internal/child_process:453:11)
409 verbose stack     at Socket.emit (node:events:365:28)
409 verbose stack     at Pipe.<anonymous> (node:net:661:12)
410 verbose pkgid @abandonware/[email protected]
411 verbose cwd D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral
412 verbose Windows_NT 10.0.19044
413 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
414 verbose node v16.2.0
415 verbose npm  v7.15.1
416 error code 1
417 error path D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
418 error command failed
419 error command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild

So I downgraded my node version using nvm from 16.2.0 to 14.17.1 (and this also downgraded my npm version to 6.14.13) and viola! My npm install worked just fine.

Upvotes: 1

Criminal_Affair_At_SO
Criminal_Affair_At_SO

Reputation: 3443

Type where node-gyp

You can have a globally installed node-gyp which you probably do and also each package can carry its own version which is probably the case with ffi The PATH determines which one will run

Upvotes: -1

Related Questions