What is the difference between --save and --save-dev?

后端 未结 13 766
旧巷少年郎
旧巷少年郎 2020-11-28 00:02

What is the difference between:

npm install [package_name]

and:

npm install [package_name] --save

and:

相关标签:
13条回答
  • 2020-11-28 00:25

    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

    0 讨论(0)
  • 2020-11-28 00:29
    • --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.
    0 讨论(0)
  • 2020-11-28 00:32

    The difference between --save and --save-dev may not be immediately noticeable if you have tried them both on your own projects. So here are a few examples...

    Lets say you were building an app that used the moment package to parse and display dates. Your app is a scheduler so it really needs this package to run, as in: cannot run without it. In this case you would use

    npm install moment --save
    

    This would create a new value in your package.json

    "dependencies": {
       ...
       "moment": "^2.17.1"
    }
    

    When you are developing, it really helps to use tools such as test suites and may need jasmine-core and karma. In this case you would use

    npm install jasmine-core --save-dev
    npm install karma --save-dev
    

    This would also create a new value in your package.json

    "devDependencies": {
        ...
        "jasmine-core": "^2.5.2",
        "karma": "^1.4.1",
    }
    

    You do not need the test suite to run the app in its normal state, so it is a --save-dev type dependency, nothing more. You can see how if you do not understand what is really happening, it is a bit hard to imagine.

    Taken directly from NPM docs docs#dependencies

    Dependencies

    Dependencies are specified in a simple object that maps a package name to a version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL.

    Please do not put test harnesses or transpilers in your dependencies object. See devDependencies, below.

    Even in the docs, it asks you to use --save-dev for modules such as test harnesses.

    I hope this helps and is clear.

    0 讨论(0)
  • 2020-11-28 00:33

    A perfect example of this is:

    $ npm install typescript --save-dev
    

    In this case, you'd want to have Typescript (a javascript-parseable coding language) available for development, but once the app is deployed, it is no longer necessary, as all of the code has been transpiled to javascript. As such, it would make no sense to include it in the published app. Indeed, it would only take up space and increase download times.

    0 讨论(0)
  • 2020-11-28 00:34

    Clear answers are already provided. But it's worth mentioning how devDependencies affects installing packages:

    By default, npm install will install all modules listed as dependencies in package.json . With the --production flag (or when the NODE_ENV environment variable is set to production ), npm will not install modules listed in devDependencies .

    See: https://docs.npmjs.com/cli/install

    0 讨论(0)
  • 2020-11-28 00:38

    You generally don't want to bloat production package with things that you only intend to use for Development purposes.

    Use --save-dev (or -D) option to separate packages such as Unit Test frameworks (jest, jasmine, mocha, chai, etc.)

    Any other packages that your app needs for Production, should 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"
    },
    
    0 讨论(0)
提交回复
热议问题