An automatic call of "node-gyp rebuild" on "npm install" and "yarn install" - npm

I am creating a package with some native code, which will be installed globally, and now I see the following behavior:
node-gyp rebuild runs automatically on local npm install and when the package is installed itself.
When I add a custom preinstall script to the package.json, it isn't invoked anymore on npm install nor when the package is installed globally, so it's not compiled at all.
However, in case of yarn install node-gyp rebuild still runs even with a preinstall hook.
Moreover, if I add "preinstall": "node-gyp rebuild", yarn runs my script and then invokes node-gyp rebuild again, so it is compiled twice.
I wonder is there any documentation describing this behavior? Cause I found nothing. I even didn't find an explicit mention that node-gyp rebuild runs automatically on npm install.
And how can I use the preinstall hook so that my module is compiled only once both for npm and yarn?
npm 6.14.6
yarn 1.22.4

Related

What does "npm rebuild" actually do?

I've been struggling with a module that does not build after running npm rebuild (namely, #getinsomnia/node-libcurl), even though it does on npm install without --ignore-scripts.
I workaround that by running npx node-pre-gyp install in the module directory, but I'd like to understand why doesn't this behave as I expect it to do - build the module.
npm docs for npm rebuild say:
This command runs the npm build command on the matched folders.
What does npm build do though? Documentation for it only exists for npm v6 and not any newer releases, I can't run npm build on my npm v8 either ("unknown command"), and even that documentation doesn't say anything to me:
This is the plumbing command called by npm link and npm install.
It should generally be called during installation, but if you need to run it directly, run:
npm build

How to install lodash.filter using npm?

I need to install lodash.filter to my Expo (React Native) project.
How should I do it using npm?
1.
npm i -g npm
npm i --save lodash.filter
npm i --save lodash.filter
I do not understand what npm i -g npm (it's updating something, but I think it will destroy my project).
$ npm i -g npm
$ npm i --save lodash
you can use second command.
npm i is the alias for the npm install and -g means gloabal
The -g or --global argument will cause npm to install the package globally rather than locally
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
Just follow the command that are written in documentation it will be fine
The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step.

npm install -g -D not updating dev dependencies in package.json

I've tried opening and closing the open package.json editor, and restarting VS Code, and running and re-running the npm install command at least 8 times but to no avail.
For some reason, the command below does not update my dev dependencies in my package.json file.
$ npm install -g -D webpack
I know I can manually add the dependency but just saying and this is driving me nuts.
Your are passing the option -g which makes npm install the package globally so you can execute the binary directly, remove that flag to install the package locally.
Can you try: $ npm install --save-dev webpack
This is the recommended way to install webpack from npmjs

Gulp doesn't work after reinstalling Windows

Upon reinstalling Windows 10 (and Node.js), the gulp command isn't found. I ran it from my root folder containing package.json and the node_modules folder. I tried npm install gulp, and also npm install: gulp command still not found. I'm a bit puzzled.
Any idea?
do you re install node and npm again? when you re install node try with npm install -g gulp
npm install -g gulp will install gulp globally, so that you can use it as a command.
A better way to do it, is to use npm install gulp (without the -g) to install it into each local project, and fire it off using npm start scripts in each project's package.json.

npm does not install new package after modifying package.json

I have a web application with some packages defined in package.json after I run npm install all dependencies are resolved. However when I add some packages after that to package.json and run npm install again nothing happens. I did the work around by typing npm install --save lodash and it downloaded the dependency and updated packages.json. It works, but let's imagine the scenario when I want to add 10 or more packages. Then it would be easier for me to just add them to package.json and run npm install but this does not work :/. So what would be the solution for the problem off adding more packages after initial npm install?
Node version: v8.1.3
npm version: 5.03
Operating System: Windows 10 Pro