Blog for Learning

This blog contains lesson material, material summary, questions and answers, definitions, types and examples, and others.

Advertisement

Powered by Blogger.

Visitors

Flag Counter

Pages

Learning Nodejs # 2: Get to know NPM for Javascript Project Management

Beginner Nodejs
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:
  1. Creating a New Project;
  2. Install modules or libraries;
  3. 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.
Make new Project Nodejs with NPM
Just fill in as is, because later we can modify again.
The command npm initwill create a file package.jsonwhose contents are like this:
Making package.json file with NPM

Get to know the package.json file

The file package.jsonis a file that contains a description of the Nodejs project. NPM needs this file to work.
File package.json with NPM
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.
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.
Node Modules
and inside package.jsonwill be added like this.
Nodejs Depedencies
In addition, the command npm installwill also create a new file namedpackage-lock.jsonThis 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 gulpinside devDepedencies.devDepedenciesare the modules that we need for application development.
sudo npm install gulp -g
Meaning: we will install modules gulpglobally and can be used on all Nodejs projects.
Anyway, if on Unix and Linux, we sometimes need a sudocommand 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.
Remove nodejs module

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.
Hello Script at NPM
In the example above, we declare the script helloto 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.
Run Script Hello with NPM
Let's try again to execute the script to run the server.
First create a file index.jsthen 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.jsHowever, because we want to execute it through NPM, we must add it in package.json.
Nodejs Server Script
After that, just be executed with the command:
npm run server
Then the server will run.
Execute the Nodejs Server Script
Now try opening the address http: // localhost: 8080 in the browser.
Execute the Nodejs Server Script
To stop the Ctrlpress 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/ .

0 Komentar untuk "Learning Nodejs # 2: Get to know NPM for Javascript Project Management"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top