The use of NPM

NPM download usage package

Step 1: Initialize the project

The project mentioned here is not a specific feature, just to create an empty folder (folder name not Chinese). Go to the root directory of the project and start the small black window (hold down shift, right click, and select “Open command line here” from the pop-up menu)

Run the following command:

Init Initializes NPM initCopy the code

It starts an interactive program that lets you fill in some information about the project and generates a package.json file. If you want to use the default information directly, you can use:

npm init --yes
// NPM init -y
Copy the code

Description:

  • This command only needs to be run once, and its purpose is simply to generate a package.json file.
  • If you already have a package.json file in your project root directory, you don’t need to run this command.
  • The package.json file can be manually modified later.

Step 2: Install the package

With the package.json file generated, we are ready to install third-party packages. There are millions of packages available on the NPM website (you need to register on the NPM website and log in to see the following data, if you just download the installation package, you don’t need to register).

The following uses the day.ja package as an example

Day.js is a package specifically designed to handle dates and times. Website: < https://dayjs.fenxianglu.cn/ >Copy the code

Run the following command:

npm install dayjs
// Install can be abbreviated to I
Copy the code

Step 3: Use packages

Once we have downloaded a package, we can use it as a core module.

Const constant name = require(‘ package name ‘)

This format is the same as that used to introduce core modules.

// Download good code from NPM, import it locally, and use it
const dayjs = require('dayjs')
console.log(dayjs);
console.log( dayjs().format('YYYY-MM-DD'));Copy the code

Use of NPM images

Mirror: copy, copy. By default, download from NPM official, slow network speed; We can go to its mirror site and download it.

Command to switch to taobao mirror

# set the mirror to Taobao. npm config set registry https://registry.npm.taobao.orgNPM config set registry HTTPS://registry.npmjs.orgNPM config get registryCopy the code

Install global packages and project packages

We use the NPM install command to install the package, which simply means downloading the package from the NPM website (or from the specified image source) to our own computer.

Global installation

The package is installed in the system directory (typically node_modules on the system disk).

npm root -g           // Check the installation directory of the global package
npm list -g --depth 0 // View globally installed packages
Copy the code

Project installation

Project installation (or local installation), where packages are installed in node_modules, in the root directory of the current project (the same as package.json)

NPM install package nameCopy the code

The difference between global installation and project installation

  • Globally installed packages typically provide commands to execute directly. We use this method to install the packages of some utility classes, such as gulp, Nodemon, Live-Server, NRM, etc.

  • Locally installed packages are project-specific, and we need to use these specific features during development.

Commands that use this package to perform tasks require a global installation. To be imported for use via require requires a local installation – project package.

Steps for installing the NRM package globally

// Step 1: Global installation
npm install nrm -g

// Step 2: List all the source information
// (*) indicates the source currently in use
nrm ls

// Step 3: Switch sources as required
// For example, specify the taobao mirror source
nrm use taotao

// Next, install the required packages normally
Copy the code

If NRM ls is not available, see here:

Blog.csdn.net/LQCV587/art…

const NRMRC = path.join(process.env[(process.platform == ‘win32’) ? ‘USERPROFILE’ : ‘HOME’], ‘.nrmrc’);

//const NRMRC = path.join(process.env.HOME, ‘.nrmrc’); Env [(process.platform == ‘win32’)? ‘USERPROFILE’ : ‘HOME’], ‘.nrmrc’);