What is the difference between --save and --save-d

2019-01-07 01:43发布

What is the difference between:

npm install [package_name] --save

and

npm install [package_name] --save-dev

What does this mean?

标签: save package npm
10条回答
霸刀☆藐视天下
2楼-- · 2019-01-07 02:11

You generally don't want to bloat production package with things that you only intend to use for development purposes. So use --save-dev (or -D) option to separate those packages such as watchers(nodemon), unit test frameworks(jest, jasmine, mocha, chai etc.etc.)

Any other library packages that are must for your app to function need to be installed using --save (or -S)

npm install --save lodash       //prod dependency
npm install -S moment           // "       "
npm install -S opentracing      // "       "

npm install -D jest                 //dev only dependency
npm install --save-dev typescript   //dev only dependency

If you open the package.json file then you will see these entries listed under two different sections:

"dependencies": {
  "lodash": "4.x",
  "moment": "2.x",
  "opentracing": "^0.14.1"
},

"devDependencies": {
    "jest": "22.x",
    "typescript": "^2.8.3"
},
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-07 02:13

As suggested by @andreas-hultgren in this answer and according to the npm docs:

If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.

However, for webapp development, Yeoman (a scaffolding tool that installs a peer-reviewed, pre-written package.json file amongst other things) places all packages in devDependencies and nothing in dependencies, so it appears that the use of --save-dev is a safe bet in webapp development, at least.

查看更多
Melony?
4楼-- · 2019-01-07 02:14

I want to add some my ideas as

I think all differents will appear when someone use your codes instead of using by yourself

For example, you write a HTTP library called node's request

In your library,

you used lodash to handle string and object, without lodash, your codes cannot run

If someone use your HTTP library as a part of his codes. Your codes will be compiled with his.

your codes need lodash, So you need put in dependencies to compile


If you write a project like monaco-editor, which is a web editor,

you have bundle all your codes and your product env library using webpack, when build completed, only have a monaco-min.js

So someone don't case whether --save or --save-dependencies, only he need is monaco-min.js

Summary:

  1. If someone want to compile your codes (use as library), put lodash which used by your codes into dependencies

  2. If someone want add more feature to your codes, he need unit test and compiler, put these into dev-dependencies

查看更多
甜甜的少女心
5楼-- · 2019-01-07 02:20
  • --save-dev is used to save the package for development purpose. Example: unit tests, minification..
  • --save is used to save the package required for the application to run.
查看更多
登录 后发表回答