en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com

What is NPM, and how we use it to install-create Node.js packages

In this article, we are going to explain What NPM is and how we use it to install-create Node.js packages.

Linux Tutorial Apr 01, 21 by Nisal N 9 min Read
What is NPM, and how we use it to install-create Node.js packages

NPM is the Node Package Manager.js. 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 make a brief introduction for beginners in 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 when working with JS.

The nice thing is anyone can upload a package to the NPM registry.

Particularly popular is the react package, which installs React, a perfect JS library, to create interactive User Interfaces. On the NPMjs project page, you can see the packages most commonly used in 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:

sudo apt install NPM

This command will install Node.js (v8.10.0 in Ubuntu bionic) and NPM.

Note: Because the version of Node.js 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 installing NPM on our computer, we can now install packages to work with them or pack our own JS project and publish it to Registry.

To learn more about installing Node.js on Linux distros, have a check out our previous blog. 

Install 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, after installation, we can run the command:

create-react-app myapp

Note: In Ubuntu, you need to run the above command with Sudo. 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:

npm install <package_name>

We install the unscoped package "package_name" in the current directory where we are. 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. 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. This 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

E.g.:

npm i create-react-app

How packages and modules differ

The npm registry contains packages and modules of Node.js code 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.

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.

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 package's name and its version.

{

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

"version": "1.0.0"

}

Also, package.json can specify 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:

npm init

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

Package.json is vital for one more reason because that's 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.

About modules

As we said, the modules are node pieces.js code that does not necessarily contain package.json. In practice, modules are any file or folder within our node_modules directory that we can load with Node.js' required () functions. To run the require() correctly 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 modules don't need to contain a package.json file, it implies that not all Node.js modules packages. But modules that have 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 module in the npm registry, there must be a package.json (so that it is a package) in its folder 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:

npm init

For scoped package, and 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 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 the npm registry:

npm publish

or if it's scoped:

npm publish --access public

Now, anyone can download with npm, install our module/package, and do it require in its js.

Recap

In this tutorial, we went through what exactly is NPM and how we can utilize it to create Node.js packages. We went through the installation process of NPM, the difference between scoped and unscoped packages and package modules: how to create and use them. 

We hope you liked the article, if so don't forget to share it with your colleagues and if you come across any errors, let us know in the comments below. 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment