node.jsnpmnpm-installasdf

Error: Cannot find module 'node-gyp/bin/node-gyp.js' in macos


whenever I use any command in npm I get below error

npm --version
/usr/local/lib/node_modules/npm/lib/cli.js:48
    throw err
    ^

Error: Cannot find module 'node-gyp/bin/node-gyp.js'
Require stack:
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/make-spawn-args.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script-pkg.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script.js
- /usr/local/lib/node_modules/npm/node_modules/pacote/lib/dir.js
- /usr/local/lib/node_modules/npm/node_modules/pacote/lib/git.js
- /usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetcher.js
- /usr/local/lib/node_modules/npm/node_modules/pacote/lib/index.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/metavuln-calculator/lib/index.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/audit-report.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/audit.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/index.js
- /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/index.js
- /usr/local/lib/node_modules/npm/lib/npm.js
- /usr/local/lib/node_modules/npm/lib/cli.js
- /usr/local/lib/node_modules/npm/bin/npm-cli.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1365:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/make-spawn-args.js:4:37)
    at Module._compile (node:internal/modules/cjs/loader:1688:14)
    at Object..js (node:internal/modules/cjs/loader:1820:10)
    at Module.load (node:internal/modules/cjs/loader:1423:32)
    at Function._load (node:internal/modules/cjs/loader:1246:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Module.require (node:internal/modules/cjs/loader:1445:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/make-spawn-args.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script-pkg.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script.js',
    '/usr/local/lib/node_modules/npm/node_modules/pacote/lib/dir.js',
    '/usr/local/lib/node_modules/npm/node_modules/pacote/lib/git.js',
    '/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetcher.js',
    '/usr/local/lib/node_modules/npm/node_modules/pacote/lib/index.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/metavuln-calculator/lib/index.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/audit-report.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/audit.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/index.js',
    '/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/index.js',
    '/usr/local/lib/node_modules/npm/lib/npm.js',
    '/usr/local/lib/node_modules/npm/lib/cli.js',
    '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
  ]
}

I tried to install node-gyp using command npm install node-gyp but that also failed with same error.

Then I tried the command sudo node-gyp configure but it also failed.

sudo node-gyp configure
gyp info it worked if it ends with ok
gyp info using node-gyp@9.4.0
gyp info using node@22.18.0 | darwin | arm64
(node:24202) [DEP0060] DeprecationWarning: The `util._extend` API is deprecated. Please use Object.assign() instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
gyp info find Python using Python version 3.13.5 found at "/opt/homebrew/opt/python@3.13/bin/python3.13"
gyp http GET https://nodejs.org/download/release/v22.18.0/node-v22.18.0-headers.tar.gz
gyp http 200 https://nodejs.org/download/release/v22.18.0/node-v22.18.0-headers.tar.gz
gyp http GET https://nodejs.org/download/release/v22.18.0/SHASUMS256.txt
gyp http 200 https://nodejs.org/download/release/v22.18.0/SHASUMS256.txt
gyp info spawn /opt/homebrew/opt/python@3.13/bin/python3.13
gyp info spawn args [
gyp info spawn args   '/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/rnatarajan/.config/nvim/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/rnatarajan/Library/Caches/node-gyp/22.18.0/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/rnatarajan/Library/Caches/node-gyp/22.18.0',
gyp info spawn args   '-Dnode_gyp_dir=/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/rnatarajan/Library/Caches/node-gyp/22.18.0/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/rnatarajan/.config/nvim',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
Traceback (most recent call last):
  File "/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/gyp/gyp_main.py", line 42, in <module>
    import gyp  # noqa: E402
    ^^^^^^^^^^
  File "/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 9, in <module>
    import gyp.input
  File "/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 19, in <module>
    from distutils.version import StrictVersion
ModuleNotFoundError: No module named 'distutils'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/rnatarajan/.config/yarn/global/node_modules/node-gyp/lib/configure.js:325:16)
gyp ERR! stack     at ChildProcess.emit (node:events:518:28)
gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:293:12)
gyp ERR! System Darwin 24.6.0
gyp ERR! command "/Users/rnatarajan/.asdf/installs/nodejs/22.18.0/bin/node" "/opt/homebrew/bin/node-gyp" "configure"
gyp ERR! cwd /Users/rnatarajan/.config/nvim
gyp ERR! node -v v22.18.0
gyp ERR! node-gyp -v v9.4.0
gyp ERR! not ok

How can I fix this error?


Solution

  • for me rm -rf /usr/local/lib/node_modules/ helped