Param Singh
npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing”
• Tool that automates the process of installing, uninstalling and updating software packages
• Provides a standard way of managing dependencies
• Managing files manually is an extremely errorprone task
Good artists copy; great artists steal - Pablo Picasso.
npm i packageName@version --save
// or
npm i packageName@version --save-dev
A package is any of:
A module is any of:
Imagine there are three modules: A, B, and C. A requires B at v1.0, and C also requires B, but at v2.0. We can visualize this like so:
Now, let's create an application that requires both module A and module C.
A package manager would need to provide a version of module B. In all other runtimes prior to Node.js, this is what a package manager would try to do. This is dependency hell:
Imagine there are three modules: A, B, and C. A requires B at v1.0, and C also requires B, but at v2.0. We can visualize this like so:
Now, let's create an application that requires both module A and module C.
Instead of attempting to resolve module B to a single version, npm puts both versions of module B into the tree, each version nested under the module that requires it.
Imagine we have a module, A. A requires B.
In npm v2 this would have happened in a nested way.
Another module C requires B, but at another version than A.
Since B v1.0 is already a top-level dep, we cannot install B v2.0 as a top level dependency. npm v3 handles this by defaulting to npm v2 behavior and nesting the new, different, module B version dependency under the module that requires it -- in this case, module C.
Module D depends on Module B v2.0, just like Module C.
However, if a secondary dependency is required by 2+ modules, but is installed as a top-level dependency in the directory hierarchy, it will not be duplicated, and will be shared by the primary dependencies that require it.
Module A to v2.0, which depends on Module B v2.0, not Module B v1.0?
Update Module E to v2.0, which also depends on Module B v2.0 instead of Module B v1.0, just like the Module A update.
We have Module B v2.0 in nearly every directory. To get rid of duplication, we can run:
npm dedupe
{
"name": "example3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mod-a": "^1.0.0",
"mod-c": "^1.0.0",
"mod-d": "^1.0.0",
"mod-e": "^1.0.0"
}
}
Updated Module A to v2.0
Now, package.json looks like
{
"name": "example3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mod-a": "^2.0.0",
"mod-c": "^1.0.0",
"mod-d": "^1.0.0",
"mod-e": "^1.0.0"
}
}
Running npm install in another system
The way that you'll use semantic versioning as the user of a package is to specify which kinds of updates you want to accept.
You don't want any changes to the api to be included in the updates that you're bringing down.
You don't want any breaking changes.
Whenever you require()a module, the following steps are executed in order:
1. If it is a core module like http, fs, path etc, load the core module.
2. If it is a relative path, load the module from the relative path.
3. Look for the module in the ./node_modules directory. If it is not there, recursively search in the parent directories’ ./node_modules until either the module is found or the root of the file system is reached.
From the source package directory:
npm link
In your consumer project directory:
npm link <package-name>
Thank You!
@paramsingh_66174