To specify which packages your project depends on, you must list dependencies and devDependencies in the package.json file of the package. When you (or other users) run NPM install, NPM downloads dependencies and devDependencies for each of those listed in package.json that conform to semantic versioning criteria. To see which version of the package will be installed, use the “Semver Calculator”.

  • Dependencies: Packages required by applications in production.
  • DevDependencies: Package for local development and testing only.

Add Dependencies to the package.json file

You can add dependencies to package.json files either on the command line or by manually editing the package.json file.

Add dependencies to the package.json file from the command line

Add dependencies and devDependencies to the package.json file from the command line. When you install them in the package root directory, Use the –save-prod flag for dependencies (the default behavior of NPM install) or –save-dev for devDependencies. Add an entry to the Dependencies property in the package.json file by running the following command from the command line:

npm install <package-name> [--save-prod]
Copy the code

Add an entry to the devDependencies property in the package.json file by running the following command from the command line:

npm install <package-name> --save-dev
Copy the code

Edit the package.json file manually

Add dependencies to the package.json file, and in the text editor, add a property called dependencies that references the name and semantic version of each dependency:

{
  "name": "my_package"."version": "1.0.0"."dependencies": {
    "my_dep": "^ 1.0.0"."another_dep": "~ 2.2.0." "}}Copy the code

Add devDependencies to the package.json file, and in the text editor, add a property called devDependencies that references the name and semantic version of each devDependency:

"name": "my_package"."version": "1.0.0"."dependencies": {
  "my_dep": "^ 1.0.0"."another_dep": "~ 2.2.0." "
},
"devDependencies" : {
  "my_test_framework": "^ 3.1.0".
  "another_dev_dep": "1.0.0-1.2.0"
}
Copy the code