DEV Community

Cover image for Node.js 101 - Understand NPM
Eric The Coder
Eric The Coder

Posted on • Updated on

Node.js 101 - Understand NPM

If you want to miss nothing follow me on Twitter: Follow @justericchapman

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.

NPM

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This package can now be use in our application

const slugify = require('slugify')
console.log(slugify('Welcome to my place', { lower: true }))
Enter fullscreen mode Exit fullscreen mode

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 //local/lib/node_modules folder. Apply -g in the install command to install package globally.

npm install create-react-app -g
Enter fullscreen mode Exit fullscreen mode

Most of the time you will need admin privilege to install a package globally. In that case use the sudo command

sudo npm install create-react-app -g
Enter fullscreen mode Exit fullscreen mode

Package Versioning and updating

We can see the package version number in the package.json config file.
package.json

"dependencies": {
    "slugify": "^1.4.7"
  }
Enter fullscreen mode Exit fullscreen mode

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)

How to install accepted update?

npm update slugify
Enter fullscreen mode Exit fullscreen mode

This command will install accepted update (if they exist at that time)

How to uninstall a package?

npm uninstall slugify
Enter fullscreen mode Exit fullscreen mode

node_modules folder
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
Enter fullscreen mode Exit fullscreen mode

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.

Conclusion

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.

Latest comments (0)