14

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.

TuringTux
  • 559
  • 1
  • 12
  • 26
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 2
    No command for just adding IIRC, but you can always edit the json yourself. Note that having a package in the package.json has nothing to do with you `require()`ing that package in code. – Chad Jul 20 '16 at 16:38
  • I understand that difference. I would like it to be there because I rely on people to run `npm install` when they clone the repo. – MadPhysicist Jul 20 '16 at 16:49
  • Just manually add it to the `package.json` file then. – Emile Bergeron Jul 20 '16 at 17:10
  • There's a really simple solution, look at this: https://stackoverflow.com/a/75015023/2876202 – jv-k Jan 05 '23 at 07:24

3 Answers3

21

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.

Andy
  • 11,215
  • 5
  • 31
  • 33
2

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.

alejandromav
  • 933
  • 1
  • 11
  • 24
-3

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.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Nossyra46
  • 15
  • 3
  • Are you saying if the package is present globally, npm eschews the local installation? – MadPhysicist Jul 20 '16 at 16:50
  • 2
    That's what he's saying, but he's wrong. `npm install foobar` will install `foobar` locally regardless of if it's already installed globally. – Emile Bergeron Jul 20 '16 at 17:13
  • Sorry for my unfortunate answer then. @EmileBergeron So how do you set your package.json to use a dependency that's already installed on you computer ? Without editing the file – Nossyra46 Jul 21 '16 at 14:48
  • 1
    Why wouldn't you just edit the file manually? It's made to be easily editable... The `--save` flag is just a convenience. – Emile Bergeron Jul 21 '16 at 14:56
  • 1
    It is not too much pain but I like to avoid doing things twice if I can. Opening the file, typing all that text that I need to memorize, making mistakes, troubleshooting, etc all lead to potential time wasted. – MadPhysicist Jul 29 '16 at 16:48
  • Why would you answer with this? Didn't you read the question. – JΛYDΞV Jan 10 '22 at 17:11