37

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
ZachB
  • 13,051
  • 4
  • 61
  • 89
Rax Wunter
  • 2,677
  • 3
  • 25
  • 32
  • I got the same error. And `npm install nan` all failed with error: `npm ERR! Cannot read property 'target' of null` – cuixiping Jul 14 '16 at 07:03

6 Answers6

72

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
    This did the trick for me too. Cloned a respository from Gitlab to a Mac that was committed on a Windows machine. Don't know if that had something to do with it. Edit: Typo – Dewald Els Dec 21 '20 at 12:59
33

Install nan globally:

npm i -g nan

And then,

export NODE_PATH=$(npm root -g)

Emre Tapcı
  • 1,743
  • 17
  • 16
  • 5
    Would be fantastic, if you could add some details why this resolves the issue, e.g. what environment are working in, what causes the problem, etc. That way we can all better understand why there is the problem and how your answer helps resolving it. – Manfred Jan 21 '19 at 00:26
  • 1
    Your answer actually solved the issue in our case. It'd be great, though, to understand why... – Manfred Jan 21 '19 at 00:34
  • should I add something that always exports that node_path? – tofutim May 08 '20 at 18:28
21

Not ideal but it works if you install nan first.

$ npm install nan
David Momenso
  • 1,931
  • 1
  • 12
  • 10
  • `nan` could be an indirect dependency. Executing `npm install nan` would then modify `package.json`. That may or may not be desired. Adding `nan` as a direct dependency seems to be a functioning workaround, though. – Manfred Nov 13 '18 at 03:57
8

I had this exact error with node.js version v12.18.3 and npm 6.14.6.

Upgrading to node.js version v14.15.4 (which includes npm 6.14.10) resolved the issue. npm install ran successfully after the upgrade.

There was no need to install nan in any way.

Risadinha
  • 16,058
  • 2
  • 88
  • 91
  • This combined with deleting the package-lock.json worked for me. I tried to remove package-lock.json first which didn't help. So I can't objectively say I didn't have to remove package-lock.json in order for this to work, but my hunch is removing the lock file is obsolete. – Ogier Schelvis Sep 10 '21 at 11:14
0

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).

-- 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.

Carl
  • 307
  • 3
  • 11
0

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

Maielo
  • 692
  • 1
  • 6
  • 20