In order to use almost any development tools based on JavaScript, you'll need to know how to use npm and Node.js.
Goals
- Learn what Node.js and npm are
- Set up Node.js and npm on Windows, Mac and Linux
What is Node.js and npm?
Node.js is free and open source server JavaScript framework and is designed to build scalable network applications using JavaScript, that's why now we can also use JavaScript as a server-side language.
npm stand for Node Package Manager, where all the free Node.js packages (or modules if you like) are at one place to download and use as required. It comes with the NodeJs Installation so not to worry.
Installation
Go to the Node installation page, and download the Node installer as per your system configuration. Be sure to install the version labeled as LTS
Once it's done, you can check both node and npm functioning by opening any shell/terminal and typing node -v
and npm -v
, which will show the current installed version number.
Now you are all set, to start any NodeJs application.
Updating NodeJS
Sometimes in future you may need it, when you have to upgrade to newer version.
For NodeJs update, Install nodeJs again form web-site. Node installation page
For updating NPM
- To update NPM :
npm install npm@latest -g
- To update NPM to a specific version :
npm -g install npm@<version>
Create a Project
Here, We create a simple project to test that everything is working properly.
Initialize Project
Navigate to the directory in which you want your project to exist - in my case, first-node-app.
cd first-node-app
Now initialize a new project with npm. Avoid -y
flag to fill the values manually
npm init -y
Now you'll notice we have a package.json file that contains all the default information. Now you can modify it directly some of the few blanks.
{
"name": "first-node-app",
"version": "1.0.0",
"description": "Creating my first Node project.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "rahul",
"license": "ISC"
}
A package.json is a file that contains metadata about the project, and handles the dependencies (additional modules) of the project.
Running node Application
Let's create index.js in the root of our project directory:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}`);
});
In your shell/terminal, run the node
command followed by the filename in the root of your project.
node index
If everything went well, we will see on console,
Server running at http://127.0.0.1:3000
Now open the link http://127.0.0.1:3000
in the browser
Packages
NodeJs has few built-in modules, but you may required other modules as well as per the application you build, so here the npm plays a big role (which is loaded with all the free Node.js packages made by developers) Downloading a package is very easy, so not to worry
Installing dependency package
We are using a demo package having few features in it, to make you understand it batter
npm install demo-hello-world-pkg
When you run this command inside the project directory, it will successfully install the dependency package by automatically creating a node_modules directory and a package-lock.json file, which you can ignore.
- node_modules - Directory where all the packages will be placed which you install
- package-lock.json - Contains the metadata about all the dependency modules your app is using now
Finally, it updates our package.json file with a new line.
"dependencies": {
"demo-hello-world-pkg": "^1.0.0"
}
Now the project knows about any dependency that you have installed
In order to use a dependency, we use require()
and put it in a variable, like
const demo = require('demo-hello-world-pkg')
This dependency has only 3 built-in module functions
printMsg
- To print a default message on consolehello
- To get a "Hello World" stringtoday
- To get the current date and time
Lets include the dependency and use its module in our code
const http = require('http');
const demo = require('demo-hello-world-pkg')
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
demo.printMsg()
res.write(demo.hello() +" Today is " + demo.today())
res.end();
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}`);
});
Now run it as earlier
node index
If everything went well, we will see on console,
Server running at http://127.0.0.1:3000
This is a demo package
Now open the link http://127.0.0.1:3000
in the browser
Local vs. Global
With npm, you will have some global installs, which we mostly do when, the modules are needed in multiple projects. but if needed any module just for the specific project then their you have to install the required module locally.
Avoid -g
flag if you don't wanna install, update or uninstall module globally
Installing
For installing any node_module,
npm install PackageName
- To install package globally :
npm install PackageName -g
- If you downloaded someones project you just wanna try then use just
npm install
, that will install all the dependency mentioned in the project's package.json file, now you can run the project
Updating
For updating any node_module
npm update PackageName
- To update latest global PackageName:
npm update -g PackageName
- To update all global packages :
npm update -g
- To update all local and global packages :
npm update
Uninstalling
For uninstalling any node_module
npm uninstall PackageName
For full NPM CLI documentation
Conclusion
In this tutorial, we learned the following:
- What Node.js and npm is
- How to install them
- How to make a local project
- How to install a dependency with npm
Well, Now you're ready to start using anything that depends on Node.js like React App, Vue App