
One tool that will often be used in Nodejs is NPM (Node Package Manager) . NPM is automatically installed when we install Nodejs .
We can use NPM to:
- Creating a New Project;
- Install modules or libraries;
- Run the command line script.
On this occasion, we will learn these three things and also recognize files
package.json
.
Let's start…
Making a New Project with NPM
First we have to provide a directory for the project. Please create a new directory with this command:
mkdir belajar-npm
Then enter the directory and type the command
npm init
.cd belajar-npm
npm init
Then NPM will ask us to fill in the project data that will be created.

Just fill in as is, because later we can modify again.
The command
npm init
will create a file package.json
whose contents are like this:
Get to know the package.json file
The file
package.json
is a file that contains a description of the Nodejs project. NPM needs this file to work.
Project information contains the name, version, and description. Then in the script section, it contains bash or command line scripts to be executed with NPM.
Besides the properties above, there are still other properties:
depedencies
contains a description of the module or library needed by the application;devDependencies
contains a description of the module or library needed for application development.
Installing Modules with NPM
If we need a module or library, we can tell NPM to install it.
The command is like this:
npm install <nama modul>
We can search for module names on the NPM website.

For example, we try to install the Momentjs module. Momentjs is a Javascript module for parse , validation, and time manipulation.
Install Momentjs:
npm install moment
This command will download the Momenjs library and add it in
package.json
.
and inside
package.json
will be added like this.
In addition, the command
npm install
will also create a new file namedpackage-lock.json
. This file is in charge of locking the installed module version.Install Module for Dev and Global
In addition to the above command, there are several arguments that are often used in installing modules:
npm install gulp --save-dev
Meaning: we will install the module
gulp
inside devDepedencies
.devDepedencies
are the modules that we need for application development.sudo npm install gulp -g
Meaning: we will install modules
gulp
globally and can be used on all Nodejs projects.
Anyway, if on Unix and Linux, we sometimes need a
sudo
command npm
to be installed globally.Removing Modules
Then how do we delete the module?
We can do this command:
npm uninstall <nama modul>
Example:
npm uninstall moment
Then the Momentjs module will be removed from the project.

Running Scripts with NPM
The next NPM capability that we should try is the ability to execute bash or command line scripts . Usually used to compile, build , run servers, testing , and so on.
How: First we must declare the script to be executed inside
package.json
.
In the example above, we declare the script
hello
to display the message Hello World!
.
Then, we can execute with the command:
npm run hello
Then the result, a message
Hello World!
will be displayed.
Let's try again to execute the script to run the server.
First create a file
index.js
then fill in the code like this:var http = require('http');
http.createServer(function(req, res){
res.end("Hello Server!");
}).listen(8080);
console.log("server running on http://localhost:8080");
We can actually execute this script directly with the command
node index.js
. However, because we want to execute it through NPM, we must add it in package.json
.
After that, just be executed with the command:
npm run server
Then the server will run.


To stop the Ctrl+ press serverC
The final word…
We already know the 3 main NPM functions in the Nodejs project. Actually there are many other functions.
As:
npm bin
npm pack
npm ping
- etc.
Everything can be checked with the command
npm -l
or you can read the NPM documentation at https://docs.npmjs.com/ .
Reference: https://www.petanikode.com/nodejs-npm/
0 Komentar untuk "Learning Nodejs # 2: Get to know NPM for Javascript Project Management"
Silahkan berkomentar sesuai artikel