The article directories

  • What is NPM?
    • Install the NPM
    • Update the NPM
    • Package. The json file
    • How to create package.json
    • The contents of the package. The json
    • Distinguish between production environment dependencies and devDependencies
    • Specifies the version of the installation package
    • Customize package.json installation package file
    • Reference file library

What is NPM?

  1. NPM (Node Package Manager): Node.js package manager, used for node plug-in management (including installation, uninstallation, management dependency, etc.), NPM is installed with Node.js package management tool, can solve many problems in node.js code deployment, common use scenarios are as follows:
  • Allows users to download third-party packages written by others from the NPM server for local use.
  • Allows users to download and install command line programs written by others from the NPM server for local use.
  • Allows users to upload their own packages or command-line programs to the NPM server for others to use.
  1. npmIs a package manager that makes it easy for javascript developers to share and reuse code. In the program development, we often need to rely on the framework provided by others, and writing JS is no exception. This repeatable framework code is calledPackage or moduleA package can be a folder containing several files and a folder calledpackage.jsonThe file.
  2. There are often dozens or even hundreds of them on a single sitepackage, scattered all over the place, it’s common to divide these packages into functional groups (similar to the split-molecule modules in Android development), but if you’re building multiple wheels, it’s better to upload onePublic platformTo allow more people to use and participate in the module of this particular function. whilenpmIt makes it easier for us to publish and download some JS wheels.
  3. You can go to the official website:www.npmjs.com/Browse and search for desired wheels directly from the command linesearchOnce in a while.
  • For example, search jQuery in NPM

  1. NPM makes it very easy to see if a dependent wheel has been updated and needs to be downloaded. Now you know what NPM is. When people talk about”npm“When,Probably saying three things:

    1. A website, which is used to search for JS modules mentioned above:www.npmjs.com/

    2. A repository that holds a large database of JS modules shared by people

    3. The client in the command line, which developers use to manage, install, and publish modules
  2. Description of grounding gas:npmSimilar to the following mobile app markets

  3. Once a developer publishes a module to the repository, others can download and use it from the NPM website or from the command line.

Install the NPM

  1. When installing Node.js, NPM is installed accordingly. Node.js already integrates NPM, so NPM is installed after installing Node.js.
  • Download the official website: nodejs.org/en/
  • Node API Documentation: nodejs.cn/
  • Check whether the local node and NPM are installed successfully

Update the NPM

  • NPM is updated much more frequently than Node, so the version of NPM shipped with node may not be the latest. You can download the latest VERSION of NPM using the following command:
NPM install npm@latest -g is equivalent to NPM install King of Glory @latest edition - Global Huawei App Market Install King of Glory @latest edition - GlobalCopy the code

or

npm install -g npm 
Copy the code
  • Install, install, install, installnpm@latestIt’s in the format of @, followed by the version number. This format is also used when downloading other modules.-gRepresents global installation.

Package. The json file

  • NPM: Node Package Manager
  • Managing local InstallationnpmThe best way to create a package is to create onepackage.jsonFile. apackage.jsonA file can have the following functions:
  1. As a description file, it describes which packages your project depends on
  2. Allow the use of semantic versioning rules (described below) to specify the version of your project dependencies
  3. Make your build easier to share with other developers and easy to reuse
  • Example: Download the jQuery package from NPM in a new empty folder
npm install jquery
Copy the code

  • JQuery is in the node_module folder:

How to create package.json

  • usenpm initCreate one in the current directorypackage.jsonFile:
  • One was created using the following question-and-answer modelpackage.jsonfile

  • The inputnpm initThen a bunch of questions pop up, you can enter the corresponding content, or you can use the default value. Enter after answering a bunch of questionsyesIt will generate what is shown in the diagrampackage.jsonFile.
  • If it’s too much trouble to answer all these questions, just type it innpm init --yesSkip the question step and generate the default valuespackage.jsonFile:

The contents of the package. The json

  • The package.json file must have at least two parts:
  1. “Name” : all lowercase characters, no Spaces, can be underlined or underlined
  2. Version: the format of X.X.X, which complies with semantic version rules.
  • Such as:
"name": "demo-package"."version": "1.0.0".Copy the code
  • Example: Initialize creation in an empty folderpackage.json



  • All of these generated by default can be modified

  • Other content:

    • Description: Indicates the description, which helps search
    • Main: entry file, usuallyindex.js
    • Scripts: supported scripts, which are empty by defaulttest
    • Keywords: keywords that help people to usenpm searchFind your item when searchingauthor: Author Information
    • License: The default value isMIT
    • Bugs: Some error information about the current project, if any
    • Can I do forinitThe command sets some default values, such as:
> npm set init.author.email "[email protected]"
> npm set init.author.name "xdr630"
> npm set init.license "MIT"
Copy the code
  • Example: Modify the package.json initialized above as follows

  • perform
npm run abc
Copy the code

  • To view

  • Example: Log the downloaded jQuery configuration into package.json
  • Folder before installation:

npm install jquery -D
Copy the code

  • Found that pcakage.json has changed

  • Install a bootstrap without it this time-D
npm install bootstrap 
Copy the code

  • Found without -d, put independenciesPhi plus minus D phi, let’s put phidevDependencies
  • throughNPM install Package nameDownloaded files will be saved in the current foldernode_module, such as:

  • Open the folder and find two files you just downloaded
  • So this is justnpmThe two packages you downloaded, both of themnode_moduleAnd the recorded information is all in the samepackage.json





Distinguish between production environment dependencies and devDependencies

  • Is in the development phase of the project is the development environment; The project went online and began to formally provide external services. The stage after going online was the production environment. In the production environment, error reporting and error logging are generally disabled.
  • devDependenciesThe development environment is configured to install the modules that the project relies on for development. Such aswebpackTools, just to build the project and package it, which is used in the development phase, but not when the project goes livewebpackTools, so you can putwebpackInstall to the development environment and use--save-devCommand install todevdependenciesCommand syntax:
npm install --save-dev packageName
# shorthand
npm i -D packageName
Copy the code
  • dependenciesThe production environment is configured to install the modules on which the project runs. For example, the jQuery library will continue to be used after the project is put online, so we need to install it in the production environment. If the required dependencies are not installed in the production environment, there may be errors when the project is put online and run. use--saveCommand install todependenciesCommand syntax:
npm install --save packageName
# shorthand
npm i -S packageName
Copy the code
  • conclusion
Configuration items dependencies devDependencies
dependencies – save shorthand -s Production environment, managed dependency packages remain in effect after the project goes live
devDependencies – save – dev shorthand – D Development environment, managed dependency packages are only available during development
  1. When using NPM to install dependencies,- saveand- save - dev
  • use- saveThe installed dependencies will be writtendependenciesBlock inside.
  • While the use of- save - devIf a dependency is installed, it will be writtendevDependenciesInto the block
  • Write nothing, only local installation, not global. It will be writtendependenciesBlock inside.
  1. inpackage.jsonIn the filedependenciesanddevDependenciesThe difference between
  • devDependenciesThe dependencies inside are only for development, not production. whiledependenciesDependent packages are available not only in the development environment but also in the production environment.
  • The two environments are specified in the configuration fileNODE_ENV=developementorNODE_ENV=productionTo specify whether the environment is development or production.
  • Some of the build tools used are for exampleGlup, webpackThese are just packages for use in development, and once they go live they don’t matter, so write itdevDependencies

Specifies the version of the installation package

NPM install [email protected] NPM install [email protected]Copy the code

Customize package.json installation package file

  • Use NPM init to create a package.json file in an empty folder and open it to add the contents, such as downloading the jQuery and bootstrap packages

  • Using the commandnpm installInstall it. This command will recognize the customizationpackage.jsonFile to install in
  • npm installI can write it as thetanpm i

Reference file library

  • In general, references are directly to the local path:
< script SRC =. /node_modules/jquery/dist/jquery.min.js'><script>
Copy the code
  • By defining a variable reference:
  • Require (name)It is directly innode_modulesTo find the folder corresponding to this name, error if not found
const$=require( 'jquery ' );
console.log($.addclass());
Copy the code