npm gets the package from the repository at https://www.npmjs.com/
npm is installed as part of nodeJs installation. Think of nodeJs as the runtime for npm.
Install npm
Install the latest version of nodeJs. npm is installed as part of nodeJs.Initialize npm config file
Go the application directory.npm init
Creates package.json that contains packages installed by npm.
Install a package
npm install <packagename> --saveCreates a node_modules folder for the module source code.
--save makes an entry for the module in Package.json
Package.json
Package.json keeps track of all packages installed (their dependencies).npm modules can re-installed/refreshed by running:
npm install
We can create named scripts in the package.json
script {
"start" : "<command line>"
The script can then be invoked as:
npm run <namedscript>
npm run build
npm run start
This is useful for builds and for starting the application.
App code sharing
When sharing code that uses npm packages, do NOT share node_modules folder. Simply share your js code and package.json. Running the the following command installs all the packages specified in package.json.npm install
Global vs Local Modules
npm modules are usually installed local to an application (folder). This is preferred to avoid any breaking changes to modules.
Modules can be installed globally. This is usually done for dev tools that are utilized for multiple projects. For example, nodemon module that detects and restarts an application (web server) may be installed globally. nodeman re-executes a script or restarts the web server when underlying source code is changed.
npm install -g nodeman
To find the location of the global npm module folder:
npm root -g
To remove a global module.
npm remove -g nodeman
Useful Commands
Check npm version
npm -vnpm --version
Get help
npmnpm help
// overview
npm help npm
Initialize npm
npm init// answer yes to all questions
npm init --yes
// set / get / delete initialization defaults
npm config set <value-name-from-package.json> "default value"
npm config get <value-name-from-package.json>
npm config delete <value-name-from-package.json>
Install/Uninstall modules
// install module and update package.jsonnpm install lodash --save
// development only package install
npm install <package-name> --save-dev
// install a module globally
npm install -g <packagename>
// get (refresh) ALL packages in package.json
npm install
// get production-only packages in package.json
npm install --production
// uninstall a package - delete from module folder and package.json
// remove and rm are synonyms for uninstall
npm uninstall <packagename>
To remove a global module.
npm remove -g <packagename>
// install a specific version of a package
npm install <packagename>@version --save
// update to the latest version
npm update <packageversion>
If the version specified in the package starts with:
^1.2.3 Update to the latest minor version. Retain the major version.
~1.2.3 Update to the latest patch. Retain the major and minor version
* Update to the latest version
To specify update to a specify version of module, enter the specific version and do not precede the version with any of the characters above. Example: "1.2.3"
1.2.3
1 is major version
2 is minor version
3 is patch version
Listing installed modules
// list all packages and their dependenciesnpm list
// list top level packages only
npm list --depth 0