NPM crash course in Cules Coding by @thatanjan

NPM crash course

What is NPM?

NPM stands for Node Package Manager. It is a package manager for Node.js. is used to install and manage packages.

What is a package?

A package is just a collection of code that you can use and share with other developers.

For example, you have created a simple function that will generate a random number.

1const generateRandomNumber = (min, max) =>
2 Math.floor(Math.random() * (max - min + 1)) + min

You might need to use this function in other projects. You can just copy-paste. But there is a better way to do this. You can create a package with the code and publish it on the web. And then you can install it in your project. If other developers want to use this function, they can just install it in their project.

A package can be just one line of code or a collection of files.

Also, packages are called modules. So, when I will mention modules in this article, I will mean packages.

Who should use NPM?

Any javascript developer. If you are a front-end dev, you might have used the library through CDN. But you can use NPM to install those libraries because a library is just a collection of packages. If you are a back-end dev and you are using Node.js, you can use NPM to install packages for node.js.

How to install NPM?

You just need to install Node.js. Check how to install Node.js for your OS.

For those who are using Arch based system, you can install Node.js through Pacman.

1sudo pacman -S nodejs
2
3# check nodejs version
4
5node --version

Check NPM version

1npm -v

It will give you the version of NPM. If you get any error that means npm is not installed.

Initialize package.json

Pakcage.json is a file that contains information about your project. We will talk about them in a minute.

First, we need to initialize package.json. Just navigate to your project folder in your terminal and type the following command.

1npm init

It will ask you some questions. You can just answer and hit enter.

If you don't want the questions, you can do this:

1npm init --yes
2
3# or you can do this
4
5npm init -y

You will have a package.json file in your project folder. It will look like this if you use default values:

1{
2 "name": "npm-try", // it will be the name of your project
3 "version": "1.0.0",
4 "description": "",
5 "main": "index.js",
6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1"
8 },
9 "keywords": [],
10 "author": "",
11 "license": "ISC"
12}

Change the default values

You can set default config options for the init command. For example, to set the default author email, author name, and license, on the command line, run the following commands:

1npm set init.author.email "example-user@example.com"
2npm set init.author.name "example_user"
3npm set init.license "MIT"

Install a package

I am going to install a package called lodash. You don't have to know what lodash is. You can check it out on npmjs.com.

1npm install lodash
2
3# or
4
5npm i lodash
1{
2 "name": "npm-try",
3 "version": "1.0.0",
4 "description": "",
5 "main": "index.js",
6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1"
8 },
9 "keywords": [],
10 "author": "",
11 "license": "ISC",
12 "dependencies": {
13 "lodash": "^4.17.21"
14 }
15}

You can see that there is a dependency object in package.json. This object contains all the dependencies that you have installed.

Dependency simply means that you have installed a package. There is another type of dependency called devDependency. We will talk about it in a minute.

The property name is the name of the package. The value is the version of the package.

You also have a node_modules folder. It contains all the packages that you have installed. It is huge.

How to use a package?

Let's say you have installed lodash. You can use it like this:

1const lodash = require('lodash')
2
3const randomNum = lodash.random(1, 10)
4
5console.log(randomNum) // 5

You can also use es6 import syntax. Just add the following line in your package.json.

1{
2 "type": "module"
3}

Then you can use lodash like this:

1import lodash from 'lodash'
2
3const randomNum = lodash.random(1, 10)
4
5console.log(randomNum) // 2

Remove a package

1npm remove lodash

Reinstalling packages

Let's remove the node_modules folder.

1rm -rf node_modules

Now let's reinstall the packages.

1npm install

This is helpful when you pull someone else's or your repository from other sources like GitHub. You wouldn't wanna push them to GitHub. So, you keep node_modules outside your version control like git.

For git, create a .gitignore file and add the following line:

1node_modules/

dependency vs devDependency

dependencydevDependency
A dependency is a package that you have installed that your actual app depends on.A devDependency is a package that is used for building the app.
It will be included in your app.It will not be included in your app.
For example, lodash, express, bootstrapFor instance, nodemon, webpack, gulp

Any package can be installed as dependency or devDependency. It depends on the project that you are building.

Install devDependency

1npm install --save-dev nodemon gulp

You can install multiple packages at once.

1{
2 "dependencies": {
3 "lodash": "^4.17.21"
4 },
5 "devDependencies": {
6 "gulp": "^4.0.2",
7 "nodemon": "^2.0.15"
8 }
9}

Remove devDependency

1npm uninstall --save-dev nodemon gulp

Npm Scripts

Npm scripts are simply a way to define a command that you can run in your terminal.

For example, if you want to run a javascript file with node you will do this,

1node index.js

Every time you need to run your file you will have to type the command. But if you use npm scripts, you can just type the name of the script.

1{
2 "scripts": {
3 "start": "node index.js"
4 }
5}

You can just now run the command like this:

1npm run start

It will be much more helpful if the command is too big. For example,

1eslint . --ext ts --ext tsx --ext js

This script will run eslint on all the files in your project. By the way if you want to learn how to set up eslint then you can watch this video.

Typing this command every time will be painful. Npm scripts make your life easy.

1{
2 "scripts": {
3 "start": "eslint . --ext ts --ext tsx --ext js"
4 }
5}

Now just run :

1npm run lint

You can add multiple scripts.

Package version

You might have seen a package version like this ^2.21.1. Let's see what this means.

  • Patch release: In this release, some small bugs get fixed. It won't change the API. So, it will not break your code.
  • Minor release: In this release, some new features are added. Most probably it will not change the API. So, it will not break your code.
  • Major release: Now some major changes are made. API will be changed. So, Most probably your code will break. But that doesn't mean everything will change.

Let's see the little signs of the package version.

When you install packages from an existing package

  • ~: This will install the package but with the minor patch release. For example, if the version is ~1.3.4 in the package.json file and you install the package then it will install the latest patch release for example 1.3.6. It will not change minor and major release versions.

  • ^: This will install the latest minor and patch release. For Example:

From:

1{
2 "dependencies": {
3 "lodash": "^4.16.5"
4 }
5}

To:

1{
2 "dependencies": {
3 "lodash": "^4.17.21"
4 }
5}
  • *: If you just put * in the version then it will install the latest version.
1{
2 "dependencies": {
3 "lodash": "*"
4 }
5}
  • No sign: If you don't put any sign then it will install the exact version.

Install a specific version of a package

Just add a @ sign after the package name and the version number.

1npm install lodash@17.13.0

Update package

1npm update lodash

Npm global packages

We have learned how to install packages locally for our project. But we can install them globally and it will be available for your whole system.

A common package is installed globally is the nodemon package. It restart our node sever when we make any changes in our code.

Install global package

Let's install the nodemon package globally.

1npm install -g nodemon
2
3# or
4
5npm i -g nodemon

Now you can run the command like this from anywhere:

1nodemon index.js

Remove global package

1npm uninstall -g nodemon

List all package

Local:

1npm list

Global:

1npm list -g

Shameless Plug

I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostAxios crash course
Next PostFetch API crash course