In this article, we are going to explain what is NPM and how we use it to install/create Node.js packages. NPM is the Node Package Manager.js which was launched in 2009 as an open-source project aimed at helping JavaScript developers share ready-made, packaged code modules for Node.js. In this article, we will make a brief intro to what is NPM and Node.js.

To know the packages and their respective versions, NPM ‘’speaks’’ with a website called Registry which includes all open source packages for Node.js. 

There are thousands of packages and modules for various uses, from developing front-end web applications (e.g. Ember), and mobile applications (e.g. Apache Cordova), to robotics platforms (e.g. Tessel 2), routers, and in general everything that you may need working with JavaScript. The nice thing is anyone can upload a package to the NPM registry.

Particularly popular is the react package, which installs React, a very good JS library for the easy creation of interactive User Interfaces. On the NPMjs project page, you can see the packages most commonly used in the various projects and there is also a GitHub project, which publishes the 1000 most popular packages of NPM every day.

Install NPM

NPM exists in the repositories of most Linux distributions. In Ubuntu/Debian it is enough to give the command to install Node.js (v8.10.0 in Ubuntu bionic) and NPM [install NPM Linux]:

sudo apt install NPM

Note: Because the version of Node.js that exists in the distribution repositories is usually older than the current version, it is best to add and use the NodeSource repositories, which offer the latest Version Node.js for Ubuntu, Debian, Red Hat Enterprise Linux, Fedora, CentOS, Linux Mint.

For example, to install Node.js v11.x in Debian is enough to add nodesource repository as root:

curl -sL https://deb.nodesource.com/setup_11.x | bash -

and then

apt-get install -y nodejs

After we installed NPM on our computer, we can now install Node JS packages to work with them or pack our own JS project and publish it to Registry.

Install Node JS packages

The installation of Node.js packages and modules is done with the command NPM install or NPM i. For example, the following command downloads from Registry and installs the create-react-app package with which you make simple react applications:

NPM i -g create-react-app

With the -g parameter we install this package "globally", which means that we will be able to use its code as an ordinary tool on our computer. In our example, that is, we can after installation run the command:

create-react-app myapp

Note: In Ubuntu, you obviously need to run the above command with sudo, otherwise the installation is not globally done. If this is a security problem, you can run with the following commands a scaffold that removes all global packages and reinstalls them but this time within a folder that is then used by npm in any future global package installation:

cd && wget https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh

chmod +x npm-g-nosudo.sh

./npm-g-nosudo.sh

However, if we did not set the -g parameter, then npm would install the package locally, which means that we could only use it as part of a project and not generally on our computer. Thus, with the command, we install the unscoped package "package_name" in the current directory where we are. :

npm install <package_name>

In fact, npm will create the sub direct node_modules where it will put the "package_name".

Scoped vs unscoped packages

With the latest commands, we asked to install unscoped packages. What's that supposed to mean? Packages in NPM are sorted into scoped and unscoped in the Node Package Manager. The former are packages that may have the same name as a package made by another npm user. But they stand out from each other thanks to the scope name, which is usually the username with which the developer has registered on npmjs.org with an @ in front. That is, with the following command we install the user's package name:

npm install @user/packagename

On the other hand, unscoped packages are always public and installed as we saw previously with npm install but without @scope in front of the package name, namely:

npm install packagename

For example:

npm i create-react-app

How packages and modules differ

As we said the npm registry contains packages and Node modules that we install with the command npm i. The difference between packages and modules is that the former contain mandatory and a configuration file called package.json while in modules this is not mandatory. In any case, however, to upload and share a package or Node.js module in the npm registry, you need to have a package.json in your project.

As we said, each npm package is described with a package.json file, which is mandatory if you ever want to publish the package to the npm registry. Within this file we define at least two things: the name of the package and its version.

{

"name": "my-awesome-package",

"version": "1.0.0"

}

Also, package.json can specify who is the creator in the author field, an abbreviated description of the project, license, website, repository, and more.

To create a package.json for our project we give the following command in the project folder and we answer the questions:

npm init

Note: With the above command we will make an unscoped package.

Package.json is important for one other reason. This is where we define whether we want the dependencies of our package from other packages as well as the versions of those packages that need to be installed together with our package.

For example, npm install <package_name>installs the latest version of each package. which is a trio of numbers of the x.x.x format, e.g. 1.0.1. For example, with the following setting we ask for my_dep with a minimum version of 1.0.0 but always in the 1.x series and another_dep with a minimum version of 2.2.0 but the same minor order:

"dependencies": {

"my_dep": "^1.0.0",

"another_dep": "~2.2.0"

},

That is, npm can install the my_dep up to version 1.9.9 and another_dep up to version 2.2.9.

Node modules

As we said the modules are node pieces or .js code that does not necessarily contain package.json. In practice, that is, modules are any file or folder within our node_modules directory that we can load with the require() function of Node.js. To run correctly the require() requires a module to be one of the following:

  • a JS file
  • a folder with an index.js file inside
  • a folder with a package.json file that contains the main field.

Finally, since it is not mandatory for modules to contain a package.json file, it implies that not all Node.js modules packages. But modules that contain a package.json are also packages.

In any case, within our program we can load a module (which exists in the node_modules subdirectory) with the command:

var req = require('module_name')

How to make a Node.js module to upload to the registry

To share our own module in the npm registry there must be in its folder a package.json (so that it is a package) and of course the file that will be loaded when the module is "called" by another application. As we saw to make an unscoped package, we give the command for scoped package:

npm init

After we have created an account in npm, we give the command:

npm init --scope=@scope-name

In both cases, the npm will ask us a few questions. In any case, we should at least fill in the questions about the name and version of the package (name, version) as well as the main field, where we need to put the name of the file we want the module to load when called by other applications or modules. In exile, the file is called index.js.

Since we now have package.json, we can now write the code of our module within the index.js. In this file, we put a function as the property of the exports object, which makes this function publicly available. For example, to print a message on the console:

exports.printMsg = function() {

console.log("This is a message from my package");

}

After we have prepared our wonderful Node.js code, we can publish the package in npm registry:

npm publish

or if it's scoped:

npm publish --access public

Now, anyone can download with npm install our modules/packages.

Conclusion

In this tutorial, we went through what is NPM and the installation process of NPM. we went through the package installation process and went through the difference between scoped and unscoped packages. 

At last, we figured out how to install Node modules and use them in the system. We hope that you liked this article and if so, let us know in the comments below. Your feedback is important for us!

People also read: