Node.js 101 - Understand NPM
Tags: javascript, node, beginners, tutorial
Published: 2021-03-14 09:24:34

---
title: Node.js 101 - Understand NPM
published: true
description: 
tags: javascript, node, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4anecy5mdl4pho8w7519.jpg
series: Node.js 101
---
If you want to miss nothing follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

I strongly recommend learning javascript first. Here a series of post I did on Dev.to: https://dev.to/rickavmaniac/javascript-my-learning-journey-part-1-what-is-javascript-role-and-use-case-49a3

I am now ready to continue my Node.js learning journey :)

Click follow if you want to miss nothing. I will publish here on Dev.to what I learn everyday. 

Without further ado here is a summary of my notes for my last day.

<h2>NPM</h2>
NPM is already pre-install with Node.js. NPM stand for Node Package Manager is a command line interface app that we use to install and manage open source packages. (Like express and thousand more)

To use NPM we have to create a config file. NPM have a command that can do that for us. Type in the terminal:
```
npm init
```
The command will ask couple of questions. Just use the default response and he will create package.json config file.

Now we will install our first package name slugify. This package is a small utility to create url web friendly from string. Ex. 'Welcome to my place' will transform to url web friendly : welcome-to-my-place

To install the package type in the terminal: 
```
npm install slugify
```
This package can now be use in our application
```js
const slugify = require('slugify')
console.log(slugify('Welcome to my place', { lower: true }))
```

NPM can also install packages globally so that all the node.js application on that computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.
```js
npm install create-react-app -g
```
Most of the time you will need admin privilege to install a package globally. In that case use the sudo command
```js
sudo npm install create-react-app -g
```

<h2>Package Versioning and updating</h2>
We can see the package version number in the package.json config file.
![package.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ga3y5cany2p31se8cmq.png)

```
"dependencies": {
    "slugify": "^1.4.7"
  }
```
The version number read like this (ex. 1.4.7):
- Major version (1) : New release with breaking changes
- Minor version (4) : New features but no breaking changes
- Patch version (7) : Only bugs fix

Version prefix:
^1.4.7 : Accept minor version update
~1.4.7 : Accept only patch version update
*1.4.7 : Accept all version update (not recommended)

<b>How to install accepted update?</b>
```
npm update slugify
```
This command will install accepted update (if they exist at that time)

<b>How to uninstall a package?</b>
```
npm uninstall slugify
```

<b>node_modules folder</b>
When you install a package, the packge content will be install in that folder. If you delete that folder you can recreate it by executing:
```
npm install
```
This command will re-install all your packages listed in packages.json config file.

This command is handy to install packages when you copy an application from a co-worker or from Github. Because when you do so the node_modules folder are never supply. So you need that command to re-create it.

<h2>Conclusion</h2>
That's it for Node.js 101. I hope you like this little series on Node.js basic. You can now use your new skill and go learn more advance concept and maybe look at package like Espress.js to help you write your first real node.js web app.