npm-logo

node package manager

is the world’s largest software registry

photo_Isaac_Schlueter
Isaac Z. Schlueter

npm was developed by Isaac Z. Schlueter
in 2009

image-box
Over 700,000 of free packages are available on the main npm registry.

Npm has 12 million users, and they download 5 billion packages a week. More than 150,000 companies,
including BBC, Coinbase, eBay, Electronic Arts, Slack
rely on npm’s products and services.

Npm consists of three distinct components:

npm-website_photo
the website
npm-registry_photo
the registry
npm-cli_photo
the Command Line Interface (CLI)

Install package

Npm is installed with Node.js.

Download and install Node.js node-website

Find packages to use on the website

npm-website

Run the following command on the command line

  • npm install <package>
  • npm install -g <package>
locally-
the module would be installed in the current working directory ./node_modules.
globally-
in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin

Uninstall a package

  • npm uninstall <package>
  • npm uninstall -g <package>

Update a package

  • npm update <package>
  • npm update -g <package>

Clear the cache

  • npm cache clean

package.json

exaple-package-json

npm init
npm init --yes

exaple-dependencies
npm install <package>
exaple-devDependencies
npm install <package> --save-dev
exaple-scripts

Steps to Publish

code => test => publish => revise code => test => publish new version ...

Create a new directory and enter the following command from terminal


						npm init												
						

Enter meaningful name and appropriate details for your package. This will create the package.json for you. All NPM packages need main key. This defines the entry point to our library. By default this will be index.js but you can change it whatever you want your entry point to be.

Publish

Once your code is thoroughly tested, it is ready to be published. Run this command from the terminal


						npm publish												
						

This will publish your package to NPM registry. If you want to make changes to your package, you have to change the version number and publish again.

npm-version

version-description
npm-scripts

Scripts

  • Used for testing, building, steamlining of the needed commands to work with a module
  • Reduce the number of configuration files
  • NPM scripts are written as usual JSON key-value pairs
  • They are simply terminal commands

NPM Scripts

  • Scripts execute automatically
  • Custom Scripts
  • Calling NPM Scripts Within Other NPM Scripts

Scripts execute automatically

  • prepublish: Run BEFORE the package is packed and published, as well as on local npm install without any arguments.
  • prepare: Run both BEFORE the package is packed and published, and on local npm install without any arguments. This is run AFTER prepublish, but BEFORE prepublishOnly
  • prepublishOnly: Run BEFORE the package is prepared and packed, ONLY on npm publish.
  • ...(full list here)

Custom Scripts


									….
										"scripts": {
											"say-hello": "echo 'Hello World'"
										}
									

Basic custom NPM script that outputs “hello world” to the consol

npm run say-hello

Calling NPM Scripts Within Other NPM Scripts


											...
											"scripts": {
											   "say-hello": "echo 'Hello World'",
											   "awesome-npm": "npm run say-hello && echo 'echo NPM is awesome!'"
											}
											
									

When you run npm run awesome-npm, the second command executes after the successful execution of the first command.

package-lock.json

It is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. The goal of the file is to keep track of the exact version of every package that is installed, especially the packages of indirect dependence. It improves the installation process.

Thanks for attention

sources used

nodejs npmjs scripts