Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override `npm install` script for NPM project

Tags:

node.js

npm

I have an NPM project, when npm install is run, I'd like to run a custom script.

I tried using this in package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "install": "./scripts/install.sh",   // <<<<
 },

but that actually just resulted in an infinite loop. The reason I am looking for this, is because there are tools that simply call npm install, so I can't control what they run. Otherwise, if I had control, I would just call ./scripts/install.sh myself instead.

Note that this is probably not the best idea, just curious if it's possible.

Note my install script looks something like this:

#!/usr/bin/env bash

export FOO="bar";
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true";

npm install
like image 822
Alexander Mills Avatar asked Feb 19 '18 03:02

Alexander Mills


People also ask

How do I force npm to install dependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

How do I force npm to specific version?

With NPM we also have other options for specifying the version of a package. Using either a caret ( ^ ) or a tilde ( ~ ) we can specify the latest minor or patch version, respectively. This way you can specify a compatible package version, but still get the latest.

How do I run a script after npm install?

You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.


1 Answers

Use preinstall to run code before npm install. Don't try to override npm install in this fashion where you would end up with an infinite loop of calls to npm install.

You can also set environment variables using the config property of package.json. See docs for details

like image 86
Jay Avatar answered Sep 18 '22 11:09

Jay