47

I upgrade my node version from v7.1.0 to v9.4.0. After this m trying to run my server then I get this.

was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 59. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

then I know about npm rebuild. I run both command npm rebuild and npm install. It fixed after run npm rebuild but I do not understand what it does. Please explain about npm rebuild

thank you

Gaurav Paliwal
  • 1,556
  • 16
  • 27
Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21

1 Answers1

69

npm install: It is obvious that npm install is used to install packages using the package.json file, this command also installs the other packages on which the packages (in package.json) are dependent. On the backside, this command uses the npm build which helps to build the packages you are installing.

npm rebuild: As the name rebuild, this command again builds the packages, used only when you upgrade the node version and must recompile all your C++ addons with the new binary.

Gaurav Paliwal
  • 1,556
  • 16
  • 27
  • 6
    Do note, however, that `npm install` succeeds if optional dependencies fail to build, but `npm rebuild` stops after the first failure. Leaving the rest of the packages not being rebuilt. One example of such optional dependency would be `ripe@0.2.1` that ultimately depends on `utf-8-validate@1.2.2`. The latter fails to build on my machine. But nevertheless that's an optional dependency, so `npm i` succeeds. – x-yuri May 29 '19 at 20:15
  • 1
    Also, if you happen to migrate to `npm-5.x` with `package-lock.json` being present, most likely `npm rebuild` won't suffice. `package-lock.json` was not respected, so you've got to not only rebuild the binaries, but also make sure that the packages are installed according to `package-lock.json`. `npm rebuild` just rebuilds what's installed. `npm i` can handle the latter. On the other hand, if a package version doesn't change, you've still got to rebuild it. So, you might need both. – x-yuri May 29 '19 at 20:15