Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM - Add to package.json but don't install

I am wondering if it is possible to run a command that would check that the package is a valid npm package, add it to package.json as a dependency, but not install it.

I am doing this because I have a certain package installed globally and need to require it for an open source project. Hence, I wish it to be included.

like image 862
MadPhysicist Avatar asked Jul 20 '16 16:07

MadPhysicist


People also ask

Does npm install automatically add to json?

Install the dependencies in the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package. json .

Does npm install look at package json?

npm ci will install packages based on package-lock. json file and if the file does not exist or does not match the packages specified in the package.

Can't install dependencies npm?

The easiest way to fix the issue is to pass an additional parameter –legacy-peer-deps to npm install. The --legacy-peer-deps tells the npm to ignore the peer dependencies and continue the installation of the package. Try the below command to install the dependencies for your project.

Why npm install is not working?

The Npm command not found error can appear when you install or upgrade npm. On Windows, the cause of this error could be that a PATH or system variable is not correctly set. The error can also occur if you do not have npm or Node. js installed, have an outdated version, or have permission issues.


2 Answers

The correct way to only update package.json, without any other side-effects is:

npm install --save --package-lock-only --no-package-lock <package>

Use --package-lock-only to prevent writing to node_modules.

The --package-lock-only argument will only update the package-lock.json, instead of checking node_modules and downloading dependencies.

Then, use --no-package-lock to prevent the creation of the lock file:

The --no-package-lock argument will prevent npm from creating a package-lock.json file. When running with package-lock's disabled npm will not automatically prune your node modules when installing.

See npm install docs for more.

like image 198
Andy Avatar answered Oct 05 '22 08:10

Andy


I don't think yo can do that with npm. I've looked into the docs and I didn't find anything about.

You can use this as a workarround:

npm i <package> --save && npm uninstall <package>

Hope it helps.

like image 30
alejandromav Avatar answered Oct 02 '22 08:10

alejandromav