About npm package manager are the most commonly used content here

Nodejs born, to front-end development of the world has brought enormous changes.
Front-end engineering, tools, and the ability to expand to the back end.


asl

Installation Nodejs will own npm , the default package manager.

There are commonly used basic commands

  • Initialization, the establishment of the project (mainly in generating a package.json file)
npm init
  • Installation package
    if it is clone an existing project, down to the local natural first step is to install all of the dependencies.

    npm install
    // 简写:
    npm i

    If you want to install a global package,

    npm install -global <packagename>
    // 或简写
    npm i -g <packagename>
    If a local installation, only in this project, we have developed dependence and production dependent on the points
    • Production dependent (installing will be added to package.json file the Dependencies )
    npm install --save <packagename>
    // 或简写:
    npm i -S <packagename>
    • Development dependent on (installing will be added to package.json file devDependencies )
    npm install --save-dev <packagename>
    // 或简写:
    npm i -D <packagename>
  • Execute commands
    in addition to the initialization and installation package, the most common number to npm run <commandname>the.
    That task execute the command.

    What can enforce it?
    Executable command tasks are configured in the package.json file scriptsunder.
    After initialization often have a default testcommand.
    Similarly, we can add their own task content here.

    such as:

    "scripts": {
      "start": "webpack-dev-server --open",
      "lint": "eslint --ext .js,.vue src",
      "build": "node build/build.js",
    },

    In the above configuration, the key of the keys themselves easily play the latter values, according to the corresponding packet to you installed,
    as webpack-dev-server is used to facilitate the development of the front end analog interface with the server,
    eslint is grammar checker troubleshooting.
    In the parameter is passed behind the.

    In particular point it is that, npm startby default, there's a command,
    in fact, it can omit runit,
    or executed npm run start.

  • Added:
    domestic access to foreign npm server installed, the speed is slow, often will be the case of installation failure.
    Often with Taobao NPM mirrorcnpm to install.

    • First, the global installed cnpm
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    • So that you can use cnpmto install the various dependencies
      just npmreplace cnpmit, install other uses exactly the same.
    cnpm install <packagename>

    In this way, the package installation is much faster, and basically impossible to install failure.
    In addition, a package can not be used npm installed, a package installed on cnpm, anyway, when I tried to delete node_modulesa folder with the same source re-installation.


Brothers of the companion volume: nrm

When it comes to the top cnpm, use a mirror NPM visit Taobao, this is a way.

There are many other sources, nrm is a unified management tool, it can be a key switch npm package installation source.

  • Global installed nrm
npm install -g nrm
  • View source
nrm ls

Display source list:

* npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

With an asterisk *source is currently in use, you can also use the following command to see:

nrm current

// => 显示 npm
  • Switching source
nrm use <originname>

Such as:

nrm use cnpm

// 显示:
Registry has been set to: http://r.cnpmjs.org/
nrm current

// 显示:
cnpm

Note:
Here's cnpm mentioned above cnpm not the same child.

Cnpm npm here only to handover source, is above cnpm single installation tool, except that no npm publish, other related npm same.


Brothers of the companion volume: nvm

Installation of the Node , is the latest version at this time.
If you want to participate in the project is already old, it may not match the version will be environmental problems,
this time to re-install an older version of it?
When do the new project again uninstall re-install the new version?

So much trouble. Of course, there is a simple way, that is nvm, node multi-version management tools.

nvm ls

// 显示:
  * 10.15.3 (Currently using 64-bit executable)
    8.15.1

That is currently using version 10.15.3 , the installation of the two versions of the node, and the other is 8.15.1
can nvm use [version]be switched version

  • Switch Version
nvm use 8.15.1

// 显示:
Now using node v8.15.1 (64-bit)

Obviously, the installation of a little beat up, to have the latest LTS version 12.13.0 , and
how to install the latest version of it?

  • Add a new node version
    to view the available version published:
nvm ls avilable

// 显示:

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    13.0.1    |   12.13.0    |   0.12.18    |   0.11.16    |
|    13.0.0    |   10.17.0    |   0.12.17    |   0.11.15    |
|   12.12.0    |   10.16.3    |   0.12.16    |   0.11.14    |
|   12.11.1    |   10.16.2    |   0.12.15    |   0.11.13    |
|   12.11.0    |   10.16.1    |   0.12.14    |   0.11.12    |
|   12.10.0    |   10.16.0    |   0.12.13    |   0.11.11    |
|    12.9.1    |   10.15.3    |   0.12.12    |   0.11.10    |
|    12.9.0    |   10.15.2    |   0.12.11    |    0.11.9    |
|    12.8.1    |   10.15.1    |   0.12.10    |    0.11.8    |
|    12.8.0    |   10.15.0    |    0.12.9    |    0.11.7    |
|    12.7.0    |   10.14.2    |    0.12.8    |    0.11.6    |
|    12.6.0    |   10.14.1    |    0.12.7    |    0.11.5    |
|    12.5.0    |   10.14.0    |    0.12.6    |    0.11.4    |
|    12.4.0    |   10.13.0    |    0.12.5    |    0.11.3    |
|    12.3.1    |    8.16.2    |    0.12.4    |    0.11.2    |
|    12.3.0    |    8.16.1    |    0.12.3    |    0.11.1    |
|    12.2.0    |    8.16.0    |    0.12.2    |    0.11.0    |
|    12.1.0    |    8.15.1    |    0.12.1    |    0.9.12    |
|    12.0.0    |    8.15.0    |    0.12.0    |    0.9.11    |
|   11.15.0    |    8.14.1    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/download/release

Let's install some 12.13.0 it

nvm install 12.13.0    

display:

Downloading node.js version 12.13.0 (64-bit)...
Complete
Creating C:\nvm\temp

Downloading npm version 6.12.0... Complete
Installing npm v6.12.0...

Installation complete. If you want to use this version, type

nvm use 12.13.0

Tell us the installation is complete, if you want to use this version, then on typing this command ( NVM use 12.13.0 ).
You will find the latest node time, npm also supporting the update.


There is one:npx

Its main feature is that allows us to directly use the command of some packages,
you do not care about there not already installed, because it will not have to check first to install.

You can also force the installation with a parameter to specify the date or the use of existing local.

The other is the temporary installation, run out of that is deleted, the next time will npx installation.

It also provides similar nvm functionality for temporarily switch node version.

This is not how I'd use.

And npm There is also a relative yarn, do presentations and summary of it in the next one.

Guess you like

Origin www.cnblogs.com/CoderMonkie/p/all-about-npm-and-nrm-nvm-npx.html