Quick start guide to Node.Js: Part-2
Now that we have succesfully installed node on our system. We can check which version got installed by running the command node - - version.
With node, npm (node package manager) automatically got installed for us.
NPM is like a store where you can shop for open source libraries written by other developers to make your life easier. We will see how we can benefit from the code written by others.
Before going further, it is worth mentioning that Node.Js documentation can be found at Node Docs
Also, We won’t have access to browser specific API’s like DOM, cookie etc. which obviously is not needed because, we are now working outside the browser environment. However, we do now have access to the filesystem, os, network, process through modules provided by Node.Js environment.
Module System of Node.js
What is a module?
A Module(library) is a small, independent reusable unit of code. Whatever code we write in a module exists as an independent entity. It can be used in other parts of your application. The developer uses this pattern to define private and public members in the code.
Node.Js uses the CommonJs module system to support modularity in development. Every javascript file is a module in itself. To get functionality from other modules we need to require them using require( ) method. In contrast, to get modularity in browser-side we need to use import statement from ES6.
Global and Core Node modules
In Node.Js we have some global modules that we don’t need to require. E.g.
console, timer, require, process. They are available by default everywhere in your project and can be used directly. In contrast, if we want to use a core node module, we have to require it using the require( ) method. E.g. fs module, os module etc.
Some globals are true globals while others are pseudo global. The global object, console, process are all true globals. We don’t need to be in a module to access them. But to access Pseudo globals we need to be in a module. For. E.g. __filename, __dirname, module.exports, require( ).
What is the difference between a package and a module then?
A package is a collection of modules. Typically a package will have one or more javascript files(module) inside it. For.e.g. Animal package might have Aquatic.js, Terrestial.js modules inside it. Node.js use a package manager (npm) using which we can access package wriiten by others and use them in our project. What does this mean? It means, we don’t need to write everything from scratch. Say, your project needs a lot of Array manipulation and requires utility functions related to Arrays. We can just install the lodash package in our project, and use its utility functions to manipulate Arrays . This saves a lot of time and also helps in writing cleaner code.
Remember, packages can be installed globally or locally. A globally installed package will be same for all your applications/projects. It can be installed using npm install -g <package-name> . It gets installed in your systems bin directory unlike the packages that are installed locally, which are stored under node_modules directory in the working directory.
A package should be installed globally when it provides an executable command that you run from the shell (CLI), and when it is reused across projects.
Normally we will use the npx command to run an executable package (like create-react-app) to avoid installing it globally. This is useful when we need to try a package before installing it globally or locally.
That being said, in most cases we would want to install packages locally. As different parts of our projects might require different versions of a package. Installing it globally will cause an package update make changes everywhere.
To see what packages are installed globally on our system we can run the command : npm list -g — depth 0
In the next Article, i will explain some Core Node Modules and their practical use in backend app developement.
Link to next article: Quick start guide to Node.Js:Part-3