The concept and use of packages and npm in Node.js

One

concept

Nodejs in addition to its core module provides external module may also use custom third-party modules
Nodejs by a third-party modules package composition can be carried out on a set of modules by coating with interdependence unified management

You can https://www.npmjs.comfind packages
in modules similar to Python

Simple to use

Download package:

npm i 包名
(Or npm install 包名)
Insert picture description here

Can be used directly by require ()
var http=require("http");

var sd = require('silly-datetime');

var app=http.createServer(function(req,resp){
    
    resp.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
    
    var d=sd.format(new Date(), 'YYYY-MM-DD HH:mm');
    resp.write(d);
    
    resp.end();
});

app.listen(8001,"127.0.0.1");

success:
Insert picture description here


Second, npm

concept

npm is the world's largest open source ecosystem through a wide variety of package npm download
the source code (package) can https://www.npmjs.comfind

npm is a package management tool installed with NodeJS and can solve many problems in NodeJS code deployment

Common usage scenarios:

  • Allow users to download third-party packages written by others from the NPM server for local use
  • Allow users to download and install command-line programs (tools) written by others from the NPM server for local use (such as supervisor)
  • Allow users to upload their own packages or command line programs to the NPM server for others to use

Common commands

Check the npm version:
npm -v

Insert picture description here

Install the module:
npm install 模块名

Example:npm install jquery
Insert picture description here

Uninstall the module:
npm uninstall 模块名

Example:npm uninstall jquery

Of course, you can also delete the folder directly
Insert picture description here

View all packages installed in the current directory:
npm list
Check the version of a package:
npm info 模块名

Insert picture description here

Install the specified version of the package:
npm install 模块名@版本

Insert picture description here


Published 194 original articles · praised 8 · 710,000 views

Guess you like

Origin blog.csdn.net/Piconjo/article/details/105573064