npm basic commands
Tags: nodjs, webdev, javascript
Published: 2022-01-15 08:55:22


![Photo by Paul Esch-Laurent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjw6w3l8eu2svo2lvyfp.png)

## npm

NPM a.k.a. 'Node Package Manager' is a registry of libraries for use in the development of javascript based applications. This is the default and largest package manager in the world.

The npm was originally invented by Isaac Z. Schlueter 11 years ago. Continued releases with the support of developers made a huge contribution in making npm the best version it is today.

NPM basically comes with the Node.js runtime environment and as a dependency. Many developers use Node.js with npm effectively to build apps quick and easily. Learning npm would be a big opportunity in the job industry as well.

npm mainly consists of,

- Libraries of packages (registry)
- Package Manager (cli)
- Installer

### npm package registry ->

The npm package registry is a database of javascript packages or libraries. These packages takes the structure of a repository which can be seen as modules, readme files, metadata etc. These libraries are available to use by anyone for their projects. Many libraries are available to open source. We can access the registry by using the npm's cli.

### npm package manager ->

The npm has its own multi-platform command line interface and it has many uses for the developer. To get npm cli we have to download and install Node.js. Click [this](https://nodejs.org/en/download/).
The npm can be referred to this cli due to the distributed architecture of it. 
The cli can be used to,

- login to npm
```
npm-cli-login
```

- initialize nodejs project 
(create 'package.json' file)
```
npm init
```

- run custom application based scripts 
(server/index.js/app.js)
```
npm run server
```

- start application
```
npm start app
```

- test your code
```
npm run tests
```

### npm installer -> 

This part of npm downloads and installs the npm packages to Node.js development project when required.
First we need to initialize our app's package.json file.

`npm init --y`

This command makes a package.json file with default  values engaged with the project's metadata. We can edit this later on ourselves. To get the initial dependencies run the installer by adding dependencies to the package.json file by adding,


![package.json file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs8mhz3i470m74q8vgpd.PNG)

You can also add the version as the value of package name. 
Then we can run the command,

`npm install`

To automatically (without editing the package.json file first) install and edit the package.json by itself, we can use the command below.

`npm i --save <package_name1> <package_name2>`

Then a folder with packages named as node_modules should be installed to the location the command used in (project root directory).
After that we can use the modules. 

Developers can also use developer dependencies like nodemon, karma etc. by adding `-dev` flag.

`npm install -dev <dev_package_name>`

We are now ready to code with the aid of newly added dependencies. By using such efficient tool we can reduce the development and reduce time by without using external plugins.

Learn more about npm with following links.

https://docs.npmjs.com/