Node Package Manager(NPM)

Node Package Manager(NPM)

·

6 min read

Understanding of NPM

What is NPM?

  • Node Package Manager.
  • NPM is a necessary tool to manage Node.js project.
  • Online Storage + Command Line tool

What is NPM Online Storage?

  • It is a place for numerous open source libraries and tools to be uploaded.
  • We can easily search libraries and tools we need.
  • It has a big ecosystem

What is Command Line Tool?

  • We can Install tools and libraries from online storage using command line tool.
  • We can also set up and manage our project.
  • We can manage dependencies for our project.

Let's use NPM

What does "use NPM" mean?

  • It means we learn how to use command line tool.

Create Project

npm init

image.png

image.png

  • This command creates a project directory
  • In the directory, It makes package.json file after some of questions.
  • This project directory is Node.js project.

What is package.json?

  • It is a file that information associated with project is saved.
  • We can remote this file directly or by npm command line.

package.json's components

  • version : project version.
  • name : project name.
  • description : project description.
  • scripts : It is a custom script we can run by "npm run [script name]" command.
  • dependencies
  • devDependencies : Dependencies only used in development environment.

Dependency Management

  • It is a way to manage dependencies used in our project.
  • Because we are relying on many libraries, We say these libraries are "dependency".

What is library?

  • This chunk of code operates specific functions.
  • We use complicated functions we need from other people's code of online storage.
  • library is called "package" in Node.js

Let's install packages from online storage!

Add & Download dependency

npm install [package-name]
  • We can add packages we need.
  • Added packages are added in dependencies of package.json file.
  • library code is saved in node_modules.

Dependencies and devDependencies

npm install [package-name] --save-dev
  • We can separate dependencies for development environment from production environment.
  • dependencies for development environment are added in devDependencies of package.json file.

package-lock.json

  • This file fixes the version of packages in dependencies.

Version Notation

npm install [package-name]@[version]
  • ~1.13.0 -> installl version 1.13.x
  • ^1.13.0 -> install version 1.x.x
  • 0.13.0 -> install 0.13.0

Download all Dependency

npm install
  • This command download packages defined in package.json.

Download production dependency

npm install --production

Add global packages.

npm install [package-name] --global

Local package and Global package

  • Local package : packages declared in package.json file and saved in node_modules
  • Global package : packages installed by "npm install -g" command and saved in global package stoage in our computer.

What should we use between local and global package?

  • For the good management of our project, We should use local package.

Remove dependency

npm remove [package-name]

  • We can remove dependency package.
  • This removes packages from dependencies and devDependencies of package.json file and node_modules folder.

Execute Script

  • package.json image.png

  • runimage.png

  • Script is code that operates simple behaviors.

  • It is declared in package.json
  • We can run the script in package.json using "npm run [script-name]"

Why do we use npm script?

// 1
"scripts" : {"test" : "node_modules/.bin/tap test/\*.js"}

//2
"scripts" : {"test" : "tap test/\*.js"}
  • We don't have to write where node_modules folder is. We can just access to packages easily like number 2

Common Scripts

  • test : unit test
  • start : project start
  • stop : project stop

NPM Summary

  • npm init : Create project
  • npm install : Add dependencies
  • npm remove : remove dependencies
  • npm run : run script
  • node_modules directory : Directory that chunks of packages are saved.
  • package.json file : project management (Version, dependencies..)
  • package-lock.json : Check dependency version.

NPX

What is NPX?

npm i cowsay -g
cowsay hi
npx cowsay hi
// We can choose specific node.js version
npx node@12 index.js
npx node@14 index.js
  • This is a tool we can use npm package without installing it.
  • We can execute npm package right way not adding the packages to our project and global package storage.

Execute code in github gist

npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32

Node.js Module

What is Module?

  • Module is a solution to split code into separate functions.

image.png

  • We separate B code as a module because It is reused in all codes.

Module and Packages

  • A chunk of modules is a package
  • So npm packages are having a chunk of many modules.
  • package > module

Default Provided module in Node.js

console

// Express log level
console.log();
console.warn();
console.error();

// time trace
console.time();
console.timelog();
console.timeEnd();

Process

  • This provides arch, argv, env associated with execution environment.
  • This also provides abort, kill, exit associated with process behavior.

fs

  • We use this for file input and output.
  • readFile : read file, writeFile : write file
  • Provide Sync function for synchronous operation.
  • Detect file/directory changes using watch.

http

  • We use this for http server and client.
  • Create server using createServer function.
  • Create http request using Request function.

Writing and Use of Module

How to declare module in code?

const name = ‘elice’;
const age = 5;
const nationality = ‘korea’;
module.exports= { 
  name, 
  age, 
  nationality ,
};
---
const student = require(‘./elice’);
// print student -> { name: ‘elice’, age: 5, nationality: ‘korea’}

Basic writing of module

module.exports= { 
  name, 
  age, 
  nationality ,
};

Writing module by variable name

constname = ‘elice’;
constage = 5;
constnationality = ‘korea’;

exports.name = name;
exports.age = age;
exports.nationality = nationality;

How to export a function?

module.exports= (name, age, nationality) =>{
  return { name, age, nationality,};
}
---
const student = require(‘./elice’)(‘elice’, 5, ‘korea’) ;
// print student -> { name: ‘elice’, age: 5, nationality: ‘korea’}
  • We can make a module a function.
  • So we can give arguments to modules when we declare the module.

How to use module?

require("module path");

const dayjs = require("dayjs"); // npm package
console.log(dayjs());

const myModule = require("./my-module"); // my-module/index.js
console.log(myModule);

const myFunctionModule = require("./my-function-module"); // call functional module
console.log(myFunctionModule(name, age, nationality));

How to call Json file?

const myDataa = require("./my-data");
console.log(myData);
  • Json files are parsed into object automatically.

Did you find this article valuable?

Support Junho Dev Blog by becoming a sponsor. Any amount is appreciated!