-->

Error: Cannot find module 'nan'

2020-06-02 17:03发布

问题:

I'm working on native Node.js addon and following nan docs

I included nan into binding.gyp like: "include_dirs" : [ "<!(node -e \"require('nan')\")" ]

Also nan is in npm dependencies.

But when I install the package inside the another node module node-gyp is failed with error

> nnb@1.0.2 install /Users/Shopgate/sandbox/stress/node_modules/nnb
> node-gyp rebuild

module.js:338
    throw err;
          ^
Error: Cannot find module 'nan'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at [eval]:1:1
    at Object.exports.runInThisContext (vm.js:74:17)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:460:26)
    at evalScript (node.js:431:25)
    at startup (node.js:90:7)
gyp: Call to 'node -e "require('nan')"' returned exit status 1. while trying to load binding.gyp

回答1:

Install nan globally:

npm i -g nan

And then,

export NODE_PATH=$(npm root -g)



回答2:

In my case, it was because of some dependencies missing in the package-lock file. I solved it by removing it and running $ npm install again.



回答3:

Not ideal but it works if you install nan first.

$ npm install nan


回答4:

Unfortunately I had this issue, anything that had 'npm' in it was immediately returned with thrown error.

module.js:471
    throw err;
    ^

Error: Cannot find module 'number-is-nan'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npmlog/node_modules/gauge/node_modules/string-width/node_modules/code-point-at/index.js:2:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
cd: no such file or directory: /npm

I had to remove my node & npm files

uninstall and reinstall instruction I followed were here (stackoverflow):

How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

I then followed the rabbit trail all the way through every issue!

Don't immediately jump to SO (stackoverflow) at the first sign of friction, the command window (cli) will "oddly enough" give you the right answers to everything (I did however hit about 2 hurdles I referred to SO for, mainly to be on the safe side).

  • When going through the install of node, npm, & grunt via Install Node.js and npm with Homebrew.

-- I ran into some friction with a python framework file being in a place that made homebrew throw a warning resolve with.

-- and a kegs link warning resolved with my own write up here.

  • Then when I needed to run: brew install node

-- I had another brew link issue, easy enough, as my write-up above for kegs link warning would just need to be walked through again, so I thought. I then was getting an error saying I can not brew link --overwrite node that file (privileges or something if I recall, none-the-less resolved here).

Finally

  • It was at this point everything was working well. -- Remember! there were simple steps inbetween that I literally just obeyed the command prompts in the cli, such as:

  • brew wanting me to delete files via a given path:

-- open finder> [at top of MacBook "monitor tool bar"] Go>go to folder...>copy&paste the path supplied by brew in cli window>it will find that director/file then delete appropriate directory/file

-- continue these steps until brew update is completely satisfied.

  • As I said this final part was just simple following of direction from brew responses directly.

  • I hope this walkthrough helps someone bypass the issues I had a bit faster, and thank you to the community that spent their time helping me with solutions that I have linked to in this answer.

  • don't cry over spilt code, keep supporting the community.



回答5:

I am little bit late for a party. But what worked for me was to re-install all node_modules using rm -rf node_modules && npm install