node-gyp: Cannot find module 'C:\...\node-gyp\bin\node-gyp.js'

Hi! I’m trying to use node-gyp but when I call node-gyp in shell it throws me this error

if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" build )  else (node "" build )
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\npm\node_modules\node_modules\node-gyp\bin\node-gyp.js'
←[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:725:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}

What should I do? I tried to install windows-build-tools originally npm i -g windows-build-tools it installs for eternal but npm i -g windows-build-tools@4.0.0 works

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 22 (9 by maintainers)

Most upvoted comments

According to this https://github.com/nodejs/node-gyp/issues/2272, you should try to run npm config delete node_gyp.

My thoughts are that somewhere in configs is a line which tells to npm to use 'C:\Program Files\nodejs\node_modules\npm\node_modules\node_modules\node-gyp\bin\node-gyp.js' path which contain excess node_modules path segment. The above line should delete wrong line from config

Error: Cannot find module 'nopt'
Require stack:
- /usr/share/nodejs/node-gyp/lib/node-gyp.js
- /usr/share/nodejs/node-gyp/bin/node-gyp.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/usr/share/nodejs/node-gyp/lib/node-gyp.js:4:14)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/share/nodejs/node-gyp/lib/node-gyp.js',
    '/usr/share/nodejs/node-gyp/bin/node-gyp.js'
  ]

What forked in my case was, removing the path

sudo rm -rf /usr/share/nodejs/node-gyp/

Then reinstalling the package no need to change the version of npm or node.

npm i node-gyp -g

another my guess is that, as i saw, you have added wrong node-gyp to your PATH variable: you have added node-gyp from internals of npm, but not from global node_moduels location (by default %APPDATA%\npm). When you have run node-gyp it has executed node-gyp from internals of npm and because npm uses magical node-gyp.cmd to launch the node-gyp itself, you got such strange error.

The solution is:

  1. press Win + R
  2. type SystemPropertiesAdvanced.exe and press Enter
  3. press Environment Variables button in bottom right corener of the window
  4. select Path var in User variables area and press Edit
  5. find row which contain appdata\roamning\npm, select it and press up button on the right side of window so many times until the line will not be higher than all ones which contain C:\Program Files\nodejs\node_modules\npm

By moving this line up, we raising priority of this path, that mean windows will earlier check this path than others (especially wrong one).

After doing this restart your terminals and try to run node-gyp as in original comment. But i have heard that it is not recomended way to do the stuff. better run commands via npx

PS. more clear guide how to change env vars