Emyl
Emyl

Reputation: 10536

Clarification of the --save option for npm install

First experiences with node.js/npm. From the npm-install docs I read:

npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:

But I can't understand how it works in practice. If, for example, I run the command:

npm install bower --save-dev

I'd expect to find a package.json file in the current directory with devDependencies set to the installed version of bower, instead I find nothing.

Am I doing/expecting something wrong?

Using node v0.10.21, npm 1.3.12 on Ubuntu 12.04 x64

Upvotes: 8

Views: 3195

Answers (2)

gprasant
gprasant

Reputation: 16023

npm install only fetches the packages from the registry and puts them in your ./node_modules. It updates your package.json to register this new dependency if you tell it to. Your package.json has three dependency blocks :

  1. dependencies - these are needed for your app to run.
  2. devDependencies - these are needed for the developer environments for your app (this is how your teammates can get the packages that you recently added to the project. The dependencies listed here are not required on production hosts.)
  3. optionalDependencies - These packages are optional and it is OK if npm cant reolve the package to install. i.e a build failure does not cause npm install to fail (Note however, that your app should handle cases where the package cannot be found.)

Here is the behavior with the different usages of the npm install command:

$ npm install async #Only installs, no change made to package.json
$ npm install async --save #Installs, adds async@version to dependencies block
$ npm install async --save-dev # Installs, adds async@version to the devDependencies block
$ npm install async --save-optional # Installs, adds async@version to the optionalDependencies block

Upvotes: 4

bevacqua
bevacqua

Reputation: 48476

npm won't create package.json for you, but it will create the necessary dependencies for you as long as package.json exists and is legal JSON.

Create it like so

echo {} > package.json

Then, doing npm i --save whatever will add whatever@~x.x.x as a dependency as expected. The file needs to be there, and be JSON, that's it.

npm.png

Upvotes: 7

Related Questions