April 01, 2020

Part 2: Node JS Tutorial- Understanding npm

Write your own module
Open visual studio code, create a new file with the name my-module.js. Let's create another file module-demo.js. We will declare a variable inside my-module.js and try to access it from module-demo.js.

In my-module.js, we have declared variable mytext.



Now we will access mytext in module-demo.js.

To run the code, go to View->Terminal

In the terminal when we execute 'node .\module-demo.js' command, mytext from my-module.js is fetched and its value is printed on console.

Manage third-party packages with npm

Packages are one or more modules bundled together. Lodash is one of the most popular packages. To use this package we need to first install it.

To install Lodash, go to the Terminal and execute 'npm install lodash'.


After its executed successfully, you will notice a new folder node_modules (if its not present) will be created and there will be another folder lodash inside it. This lodash folder has several JavaScript files that provide us with all of the functionality and features of Lodash.

Let's create a new file with name 'demo.js'. To use loadash we will use 'require' (here we don't have to specify a location, since Node already knows the default location for its modules).

If you execute 'node .\demo.js' terminal, a random number will be printed on console.

There are several different types of npm packages, and some will work as command line interfaces. Let's one of them.

Nodemon is a command line interface, we will install it first. To install any command line interface, we need use 'global' flag (append '-g' inside the install command). Let's execute 'npm install -g nodemon' from terminal.


Since Nodemon is installed globally, you will not find any folder with name 'nodemon' inside 'node_modules'. To use it we need to use 'nodemon' instead of node in commands.

When we execute 'nodemon .\demo,js', it will print the random number. Now if I make any change in 'demo.js' and hit save button, the changes will automatically reflect on command prompt because of nodemon. The nodemon detects if any change is made, and automatically executes it.

What is the package.json file?

  • We have installed Lodash and Nodemon, in future we might install more third packages. How can we track of what we have installed? We need to maintain a list somewhere with all the details.
  • Also what if want to distribute our app or project or put it into a git repository? It wouldn't make sense to include all of the packages we depend on because they take up a lot of space and since there n number of files and folders related to packages, which takes a long time to transfer those. 
  • Apart from this the the developer will have to manually install all of those packages. It will take a lot of time to execute all of them with 'npm install'.
  • To solve these problems we can create a package.json file, which stores a list of the packages our project is depended on. By doing this whenever we execute 'npm install', it will go through that list and install everything automatically.

How can we create package.json file?
It's pretty easy, go to the terminal and execute 'npm init', this will ask us several questions like package name, version, description etc, go ahead and use all of the defaults.

After it's executed successfully, you will see a package.json generated.

'npm init --yes' is another shortcut to create package.json with defaults.

-K Himaanshu Shuklaa..

No comments:

Post a Comment