Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dev dependencies vs dependencies in node.js

Tags:

node.js

npm

In a node project I found 2 kind of dependencies:

    "dependencies": {
        "axios": "0.9.1",
        "express": "4.13.4",
        "lodash": "4.6.1",
        "react": "^0.14.7",
        "react-dom": "^0.14.7",
        "react-redux": "^4.4.0",
        "react-router": "^2.0.0",
        "redux": "^3.3.1"
      },
      "devDependencies": {
        "babel-core": "^6.5.2"
}

I know the author install it via npm install babel-core --save -dev

but what is that for? when you push your code, the devDependencies module is still there.

like image 708
Maria Jane Avatar asked Sep 27 '16 05:09

Maria Jane


People also ask

What is the difference between Dev dependency and dependency?

The difference between these two, is that devDependencies are modules which are only required during development, while dependencies are modules which are also required at runtime. To save a dependency as a devDependency on installation we need to do an npm install --save-dev , instead of just an npm install --save.

Is npm A dev dependency?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

What are dev dependencies in package json?

devDependencies: This property contains the names and versions of the node modules which are required only for development purposes like ESLint, JEST, babel etc. To install a node module as devDependency: npm install --save-dev [npm package name]

What are dependencies in node JS?

The dependencies value is used to specify any other modules that a given module (represented by the package. json ) requires to work. When you run npm install from the root folder of a given module, it will install any modules listed in that dependencies object.


1 Answers

I wrote an article about this however it was taken down.

Snippets from the article:

mod-a
  dev-dependents:
    - mod-b
  dependents:
    - mod-c

mod-d
  dev-dependents:
    - mod-e
  dependents:
    - mod-a

----

npm install mod-d

installed modules:
  - mod-d
  - mod-a
  - mod-c

----

checkout the mod-d code repository

npm install

installed modules:
  - mod-a
  - mod-c
  - mod-e

Publishing to npm

If you are publishing to npm, then it is important that you use the correct flag for the correct modules. If it is something that your npm module needs to function, then use the "--save" flag to save the module as a dependency. If it is something that your module doesn't need to function but it is needed for testing, then use the "--save-dev" flag.

# For dependent modules
npm install dependent-module --save

# For dev-dependent modules
npm install development-module --save-dev

Not for npm

If you aren't publishing to npm, it technically doesn't matter which flag you use. However, I find it a good practice to use the "--save" flag for modules that introduce non-standard code into the source files. Then use the "--sav-dev" flag for modules that are required for your compiler to function.

# For modules that introduce non-standard source code
npm install source-module --save

# For modules that your compiler needs to function
npm install compiler-module --save-dev
like image 128
Daniel Tonon Avatar answered Sep 25 '22 05:09

Daniel Tonon