Sunday, June 21, 2020

Handy npm commands

1. use npm init to generate a package.json file for the application

2. use npm install --save flag to mark the package as an application dependency.

for example when i ran npm install express --save, the process will automatically add express to the
dependencies section.

use npm install --save-dev to install the library for the development of an application only

{
  "name""first_express_project",
  "version""1.0.0",
  "description""first web project build with express",
  "main""main.js",
  "scripts": {
    "test""echo \"Error: no test specified\" && exit 1"
  },
  "author""Dan Deng",
  "license""ISC",
  "dependencies": {
    "express""^4.17.1"
  }
}

3.  run npm docs library name to access the library documentation.

for example npm docs express will launch the default web browser to http://expressjs.com.

4. npm i nodemon -g to install the node monitor package to force restart the application. after any change to the project.
use the following configuration to setup nodemon to run the application

 "scripts": {
    "start":"nodemon main.js",
    "test""echo \"Error: no test specified\" && exit 1"
  },

then run npm start to launch the application.

λ npm start

> first_express_project@1.0.0 start C:\code\Node\first_express_project
> nodemon main.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node main.js`
The express server has started and is listening on port number : 3000








No comments:

Post a Comment