Packages in NodeJS

The package directory that fully complies with the CommonJS specification generally contains the following files

  • package.json: package description file
  • bin: The directory used to store executable binary files
  • lib: the directory used to store javascript code
  • doc: directory for storing documents

Download third-party modules (packages) through npm commands in NodeJS

https://www.npmjs.com/package/silly-datetime

Automatically generate package.json

npm init --yes
npm init -y

Here is a pit under mac, if the folder is Chinese, an error will be reported

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-RitqH5x0-1602482736660)(/Users/liujingping/Library/Application Support/typora-user-images/image-20201012130654358 .png)]

Use of npm

  • Open the https://www.npmjs.com/ website and find the package you need, which is the third-party module

  • Install md5 using command

    npm install md5 (最好是加上--save,这样在package.json中的dependencies中会有这个模块和版本号,方便下次使用)
    
  • File entry in const md5 = require("md5")the module incorporated md5

  • Uninstall module using command

    npm uninstall ModuleName
    
  • Use the command to view the currently installed modules

    npm list
    
  • Specify version installation

    npm install jquery@1.8.0
    

    Package.json

    package.json define this project required various modules, and configuration information items (such as name, version, license and other metadata)

    1. Create package.json

      npm init 或者 npm init –yes
      
    2. package.json file

      {
              
               
      	"name": "test", 
      	"version": "1.0.0", 
      	"description": "test", 
      	"main": "main.js", 
      	"keywords": [ "test" ],"author": 
      	"wade", "license": "MIT", 
      	"dependencies": {
              
               "express": "^4.10.1" },
      	"devDependencies": {
              
              "jslint": "^0.6.5" } 
      }
      
    3. Install the module and write the module to package.json (dependency)

      npm install babel-cli --save-dev npm install 模块 --save npm install 模块 --save-dev
      
    4. The difference between dependencies and devDependencies ?

      Use npm install node_module --save to automatically update the dependencies field value;

      Use npm install node_module --save-dev to automatically update the value of the devDependencies field;

      dependencie configures other packages that the current program depends on.

      devDependencie configures other packages that the current program depends on, such as some tools and other configurations here

      "dependencies": {
              
               
      	"ejs": "^2.3.4", 
      	"express": "^4.13.3", 
      	"formidable": "^1.0.17" 
      }
      

      ^ Means the first version number remains unchanged, and the last two digits are the latest

      ~ Means the first two digits remain unchanged, and the last one is the latest

      *Means that all take the latest

    5. Introduction and use of Taobao mirror

    http://www.npmjs.org npm package official website

    https://npm.taobao.org/ Taobao npm mirror official website

    Taobao NPM mirror is a complete npmjs.org mirror, you can use this instead of the official version (read only), sync frequency

    The current rate is once every 10 minutes to ensure that it is synchronized with the official service as much as possible.

    We can use our customized cnpm (gzip compression support **)** command line tool instead of the default npm:

    npm install -g cnpm --registry=https://registry.npm.taobao.org
    

Guess you like

Origin blog.csdn.net/qq_39208971/article/details/109026498