MadPhysicist
MadPhysicist

Reputation: 5841

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.

Upvotes: 16

Views: 10274

Answers (3)

Nossyra46
Nossyra46

Reputation: 15

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.

Upvotes: -3

Andy
Andy

Reputation: 12005

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.

Upvotes: 23

alejandromav
alejandromav

Reputation: 943

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.

Upvotes: 2

Related Questions