简体   繁体   中英

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.

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.

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.

If your package is installed globally, I don't know if npm would reinstall it if you run:

npm install --save foobar

That's what I would do to add it to package.json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM