Let's say you have two packages demo-package
and demoforsantanu
. The package demoforsantanu
depends on the package demo-package
. Here is the sample code below -
// @ssantanuberaa/demo-package exports.printMessage = function(){ console.log("Hello World"); }; // demoforsantanu import demo = require("@ssantanuberaa/demo-package"); exports.printMsg = function() { return demo.printMessage(); }
To specify the packages your project depends on, you must list them as "dependencies" or "devDependencies" in your package’s package.json
file. When you (or another user) run npm install packageName
, npm will download dependencies and devDependencies that are listed in package.json
that meet the semantic version requirements listed for each.
dependencies
: Packages required by your application in production.devDependencies
: Packages that are only needed for local development and testing.You can add dependencies to a package.json
file from the command line or by manually editing the package.json
file.
To add dependencies and devDependencies to a package.json
file from the command line, you can install them in the root directory of your package using the --save-prod
flag for dependencies (the default behavior of npm install) or the --save-dev
flag for devDependencies.
To add an entry to the "dependencies" attribute of a package.json
file, on the command line, run the following command:
npm install packageName [--save-prod]
To add an entry to the "devDependencies" attribute of a package.json
file, on the command line, run the following command:
npm install packageName --save-dev
After installing dependencies, the package.json
file will be automatically updated. The entry will be added in the dependencies
object.
To add dependencies to a package.json
file, in a text editor, add an attribute 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" } }
To add devDependencies to a package.json
file, in a text editor, add an attribute 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" }
After adding the dependencies run npm install
to update your dependencies. It will install the dependencies in the node_modules
directory.
So in our case we need to update the package.json
file of demoforsantanu
package as this package depends on another package. So the package.json file will look like this -
{ "name": "demoforsantanu", "version": "1.1.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@ssantanuberaa/demo-package": "^1.0.0" } }
Now when someone install your package demoforsantanu
, the NPM will also install all the dependencies that are mentioned in the package.json
file of demoforsantanu
.
Also, if other application (say, demo
) uses your package demoforsantanu
, that application's package.json
file will look like the following -
{ "name": "demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "demoforsantanu": "^1.1.0" } }
As you can see, the package.json
file of demo
package has also dependencies demoforsantanu
as this application depends on demoforsantanu
and demoforsantanu
depends on demo-package
.