Node Package Manager (NPM)
Table of contents
- What is NPM?
- NPM Command
- What is Package.json?
- Install Package
- Node_module
- Upload and Download Project
- Dependency Version
- List Of Dependency
- Viewing Information About Package
- Install Specific Version of a Package
- Update Packages
- Install DevDependencies
- Uninstall a Package
- Global Packages
- Publishing Our Owen Packages
- Updating Owen Packages
What is NPM?
NPM is package manager for javascript programming language.
NPM Command
Check version of npm:
npm -v
Install package globaly:
npm -i -g asd@version
What is Package.json?
Json file that keeps metadata about the project and node module and version that use in the project.
Create package.json with below command
npm init --yes
package.json:
{"name": "node_traning","version": "1.0.0","description": "","main": "app.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"}
Install Package
npm i underscore
After installing complete name and version of package add to the package.json
{"name": "node_traning","version": "1.0.0","description": "","main": "app.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC","dependencies": {"underscore": "^1.11.0"}}
Note
We use installed package with require function
Node_module
When we install a package npm has installed automatically the dependencies of the package and stored in the node_module folder. another package can use that dependency but if the version of a dependency is different, the dependency stored in package/node_module
Upload and Download Project
When we want to work with projects that download from Github or etc, we use the below command after download the code of the project
npm i
This command look at package.json and installed the dependencies
Note
we put node_module directory in gitignore
Dependency Version
suppose the below Json
{"absd" : "^4.10.2", // is equal 4.x"efgh" : "~1.5.3" // is equal 1.5.x}
If we run the npm install command for the first dependency, npm installed the latest version of the package that Major version is 4 and for the second dependency, npm installed the latest version of package that Major version is 1 and Minor version is 5.
List Of Dependency
We can see all dependency of the project with the below command
npm list
โโโฌ mongoose@5.10.15โ โโโ bson@1.1.5โ โโโ kareem@2.3.1โ โโโฌ mongodb@3.6.3โ โ โโโฌ bl@2.2.1โ โ โ โโโฌ readable-stream@2.3.7โ โ โ โ โโโ core-util-is@1.0.2โ โ โ โ โโโ inherits@2.0.4โ โ โ โ โโโ isarray@1.0.0โ โ โ โ โโโ process-nextick-args@2.0.1โ โ โ โ โโโ safe-buffer@5.1.2โ โ โ โ โโโฌ string_decoder@1.1.1โ โ โ โ โ โโโ safe-buffer@5.1.2โ โ โ โ โโโ util-deprecate@1.0.2โ โ โ โโโ safe-buffer@5.2.1 dedupedโ โ โโโ bson@1.1.5 dedupedโ โ โโโ denque@1.4.1โ โ โโโฌ require_optional@1.0.1โ โ โ โโโ resolve-from@2.0.0โ โ โ โโโ semver@5.7.1โ โ โโโ safe-buffer@5.2.1 dedupedโ โ โโโฌ saslprep@1.0.3โ โ โโโฌ sparse-bitfield@3.0.3โ โ โโโ memory-pager@1.5.0โ โโโ mongoose-legacy-pluralize@1.0.2โ โโโ mpath@0.7.0โ โโโฌ mquery@3.2.2โ โ โโโ bluebird@3.5.1โ โ โโโฌ debug@3.1.0โ โ โ โโโ ms@2.0.0โ โ โโโ regexp-clone@1.0.0 dedupedโ โ โโโ safe-buffer@5.1.2โ โ โโโ sliced@1.0.1 dedupedโ โโโ ms@2.1.2โ โโโ regexp-clone@1.0.0โ โโโ safe-buffer@5.2.1โ โโโ sift@7.0.1โ โโโ sliced@1.0.1โโโ underscore@1.11.0
If we want to see just the dependency of project
npm list --depth=0
โโโ mongoose@5.10.15โโโ underscore@1.11.0
Viewing Information About Package
We can read more about the package and data in package.json file with the below command
npm view mongoose
mongoose@5.10.15 | MIT | deps: 11 | versions: 619Mongoose MongoDB ODMhttps://mongoosejs.comkeywords: mongodb, document, model, schema, database, odm, data, datastore, query, nosql, orm, dbdist.tarball: https://registry.npmjs.org/mongoose/-/mongoose-5.10.15.tgz.shasum: 5e559467890e0883d2a1ff0470a7467a1b47e52d.integrity: sha512-3QUWCpMRdFCPIBZkjG/B2OkfMY2WLkR+hv335o4T2mn3ta9kx8qVvXeUDojp3OHMxBZVUyCA+hDyyP4/aKmHuA==.unpackedSize: 2.1 MBdependencies:bson: ^1.1.4 ms: 2.1.2kareem: 2.3.1 regexp-clone: 1.0.0mongodb: 3.6.3 safe-buffer: 5.2.1mongoose-legacy-pluralize: 1.0.2 sift: 7.0.1mpath: 0.7.0 sliced: 1.0.1mquery: 3.2.2maintainers:- aaron <aaron.heckmann+github@gmail.com>- rauchg <rauchg@gmail.com>- tjholowaychuk <tj@vision-media.ca>- vkarpov15 <val@karpov.io>dist-tags:latest: 5.10.15 legacy: 4.13.21 unstable: 3.9.7published 16 hours ago by vkarpov15 <val@karpov.io>
If we want to see just the dependency of the package, use the below command
npm view mongoose dependencies
{bson: '^1.1.4',kareem: '2.3.1',mongodb: '3.6.3','mongoose-legacy-pluralize': '1.0.2',mpath: '0.7.0',mquery: '3.2.2',ms: '2.1.2','regexp-clone': '1.0.0','safe-buffer': '5.2.1',sliced: '1.0.1',sift: '7.0.1'}
If we want to see all versions of the package use below command (sometimes you want to downgrade your package version)
npm view mongoose versions
['0.0.1', '0.0.2', '0.0.3', '0.0.4', '0.0.5',...'5.10.11', '5.10.12', '5.10.13', '5.10.14', '5.10.15']
Install Specific Version of a Package
npm install mongoose@2.4.2
Update Packages
When we want to see outdated package use below command
npm outdated
Package Current Wanted Latest Locationmongoose 4.1.1 4.13.21 5.10.15 node_traningunderscore 1.4.0 1.11.0 1.11.0 node_traning
Current means current version of package that we install. Wanted means latest update with same major release. Latest is last version of package that release. If we want to update packages, we should use below cammand
npm update
+ underscore@1.11.0+ mongoose@4.13.21added 11 packages from 11 contributors, removed 8 packages, updated 19 packages and audited 33 packages in 5.66s
Note
Update command update package to Wanted version of package
Note
If we want to update package to Latest release we use npm-check-updates package.
sudo npm i -g npm-check-updates
After installing complete run below command to check outdated packages
npm-check-updates
Checking /home/USER/Desktop/node_traning/package.json[====================] 2/2 100%mongoose ^4.13.21 โ ^5.10.15Run ncu -u to upgrade package.json
Then run below command to update packages in package.json
npm-check-updates -u
In last step run below command to install latest version of package
npm i
Install DevDependencies
When we want packages only in development and we don't need them in production we install the packages with below command
npm install jshint --save-dev
Uninstall a Package
We run below command for uninstall package
npm un mongoose
Global Packages
If you want to work with global package use '-g' command.
Publishing Our Owen Packages
When we want to publish our owen packages, we should follow this instructions:
- Create account in npmjs.org with below commandTerminalnpm adduser
- Login to your accountTerminalnpm login
- Publish your packageTerminalnpm publish
Note
If get an error change your package name.
Updating Owen Packages
If you want update your owen packages on npmjs after change your package increase package version with below command
npm version major or minor or pacth
Then publish your update
npm publish
