Eric The Coder
Eric The Coder Blog

Eric The Coder Blog

Create a backend in Javascript (part 5): Node Package Manager (NPM)

Create a backend in Javascript (part 5): Node Package Manager (NPM)

Eric The Coder's photo
Eric The Coder
·Oct 11, 2021·

5 min read

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything there is to know about Node.js

To not miss anything follow me on twitter:

NPM (Node Package Manager)

Do you want to create a NodeJS function or module? Why re-invent the wheel? Chances are, the function you want to code has already been created by another developer.

NodeJS, allows you to use libraries created by the community. To do this, there is a tool called NPM (Node Package Manager) which allows you to download, install and manage these packages.

NPM is already pre-installed with NodeJS. It is a command line application that will allow you to install the package of your choice.

The website [ contains a list of all the packages available through NPM. There are more than a million.

NPM provides a system that allows management of installed packages and their version. This management is done with the help of a file called 'package.json'

This 'package.json' file is unique to our application and contains the list of 'dependencies' of our project. That is to say the list of all the installed packages and their version.

This file also contains other information about your project such as name, author and start file (the first that Node.jS will run). It is also possible to create 'script' commands to launch certain functions such as launching the server or the compilation and execution of the project.

You can create this 'package.json' file manually or run an NPM command to do it automatically

$ npm init

This command will ask a few questions. Just use the default answers and it will create the file 'package.json'

Note that if you have not created a 'package.json' file when you install your first package, NPM will create the file automatically for you.

We are now going to install our first package named 'slugify'. This package is a small utility for creating a web friendly URL from a string. Ex. "Welcome to my home" will turn into a user-friendly URL for the web: welcome-to-my-home

To install the package enter the following command in the terminal:

npm install slugify

Once the package is installed, you can consult the 'package.json' file, you will find the 'slugify' package listed under the list of 'dependencies' there:

  "dependencies": {
    "slugify": "^1.6.0"

Note that the source files of this packages have all been copied into the 'node_modules' folder

This package can now be used in your application

const slugify = require('slugify')

console.log(slugify('My New Web Site'))
// My-New-Web-Site

NPM can also install packages globally so that any Node.js applications on your computer can import and use the installed packages. NPM installs the global packages in the //local/lib/node_modules folder.

Add -g in the install command to install the package globally.

$ npm install create-react-app -g

Most of the time, you will need administrator privileges to install a package globally. In this case, use the sudo command (on Mac)

$ sudo npm install create-react-app -g

Version management

We can see the version number of the package in the file of 'package.json'

"dependencies": {
    "slugify": "^1.4.7"

The version number reads like this (eg 1.4.7):

  • Major version (1): New version with breaking changes
  • Minor version (4): New features but no breakthrough changes
  • Patch version (7): Only bugs are fixed
  • Version prefix: ^ 1.4.7: Accept the minor version update ~ 1.4.7: Only accept the update of the patch version
  • 1.4.7: Accept all version updates (not recommended)

Updating packages

npm update slugify

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

Uninstalling packages

npm uninstall slugify

node_modules folder

When you install a package, the contents of the package will be installed in this folder. If you delete this folder, you can recreate it by running:

$ npm install

This command will reinstall all your packages listed in the file 'packages.json'

This command is useful for installing packages when you copy an app from a colleague or from Github. Because when you do so, the node_modules folder is never provided. So you need this command to recreate it.

Node Package Execute (NPX)

npx stands for Node Package execute. It is a tool specially designed for executing packages. When you launch the execution of a package with this tool, npx will look in the "PATH" variable of the computer then in the binary files of the project modules to launch the command. If it has not found it, the tool is even able to go on the web to find the command and then execute it.

The package is executed in the current directory. npx can also be used in the "scripts" section of the "packages.json" file, in order to put commands that launch a server or a platform at startup.

Launch a package with npx

npx creat-react-app my-app

The nodemon package

When you develop a NodeJS application, each time you modify your code you must stop the server and restart your application. It is a huge waste of time.

Fortunately, there is a tool to remedy this: nodemon

nodemon is a tool that helps develop Node.js based applications by automatically restarting the Node application when file changes in the directory are detected.

nodemon does not require any additional modification to your code or your development method. nodemon is a replacement wrapper for Node. To use nodemon, replace the word node on the command line when running your script.

Example use to launch / test your application:

$ npx nodemon app.js

Installation of the module in development mode only

$ sudo npm install nodemon -D

It is also possible to install the nodemon package globally. This way you can use nodemon from all your projects

Here is the command for installation

$ npm install nodemon -g

Use when globally installed

$ nodemon app.js


That's all for today, follow me on twitter: to be notified of the publication of the next article (within two days).

Did you find this article valuable?

Support Eric The Coder by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this