NPM download usage package

Divided into three steps

  • Initialize the project. NPM init can be omitted if it has been initialized previously.
  • The installation package. NPM install Specifies the package name.
  • Import modules, use.

Stay connected

Step 1: Initialize the project

The project mentioned here is not a specific function, just to create an empty folder (note, do not use Chinese name ha).

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)

Enter the following command:

NPM init --yes // -- no Spaces between yes and init, -- Spaces between init and // or NPM init -yCopy the code

The init command is used to generate a package.json file in the root directory, which contains basic information about our current project and is the start of everything.

Step 2: Install the package

NPM has good code on this supermarket, we would like to download to use —— installation 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).

Install day.to js package

Day.js is a package specifically designed to handle dates and times. Website: dayjs. Fenxianglu. Cn /


npm install dayjs
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 const dayjs = require('dayjs') console.log(dayjs); console.log( dayjs().format('YYYY-MM-DD') );Copy the code

NPM init command

To open a small black window in a directory, type 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 // or NPM init -yCopy 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.

Package. The json file

It is typically created by the NPM init command, and its overall content is a JSON string that describes the current project as a whole. The outermost layer can be thought of as a JS object (each property name is marked with a “”, which is a typical JSON tag). There is a lot of content in this file, we are currently studying the following:

  • Name: Indicates the name of the project. If it is a third-party package, it determines what we should write when require().
  • Version: indicates the version number
  • Keywords: keywords
  • Author: the author
  • Descrption: describe
Copy the code

NPM mirroring – Makes loading faster

How to Set up a Mirror

Set the image to Taobao.

npm config set registry registry.npm.taobao.org

Set the image to NPM official

npm config set registry registry.npmjs.org

Check the configuration

npm config get registry

Copy the code

The classification of the package

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. That specific package downloaded to where, or a little attention.

It falls into two categories:

  • Global installation: Packages are installed in the system directory (typically node_modules on the system disk).

    • Command: NPM install -g package name or NPM install -g package name

    • Auxiliary tips:

      • NPM root -g // View the installation directory of the global package. NPM list -g --depth 0 // View the globally installed packageCopy the code
  • 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).

    • Command:NPM install package name

The difference between global packages and project packages

  • Globally installed packages typically provide commands to execute directly. We did this by installing packages for 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.

A rule of thumb:

  • 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.
Copy the code

Install the NRM package globally

The NRM tool is used to help us switch the NPM image source.

Before is:

# set the mirror to Taobao. NPM config set registry https://registry.npm.taobao.org # set the mirror to NPM official NPM config set registry at https://registry.npmjs.orgCopy the code

With NRM, you can simplify the command above.

steps

A total of three steps

// Step 2: List all source information // (*) indicates the current source NRM ls // Step 3: Switch source as required // for example: NRM use Taotao // Next, install the package you need normallyCopy 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 NR

Copy the code

NPM project initialization

Create an empty project on your local disk and take the folder name myNpm. Note please go to the NPM website to check if the project name is already occupied. If the name is already in use, you cannot upload to NPM.

Check the way

If 404 is returned, the project name is not found on the NPM website, you can use this command. Otherwise, the description is not available.Copy the code

For the record, myNpm is already occupied by someone else, so you need to use a different name.

npm init --yes
Copy the code

Command to create a package.json file that sets the myNpm information for your project.

Complete feature development

Develop normally and finish your code. By default, index.js is the entry file for this project.

Copy the code

Switch the current NPM image source to the official website

Since we need to upload the package to NPM, make sure the current NPM source is nPMjs.org. There are two related commands.

(1) Check the current Registry configuration of NPM.

NPM config get registry # to check the current NPM registry configuration, to ensure that is https://registry.npmjs.org # if it isn't, Can use the following command to set the NPM config set registry https://registry.npmjs.org # manually registryCopy the code

(2) Or use NRM tool to set: as shown in the picture below

Connect the NPM

npm adduser  
Copy the code

This command requires three inputs to connect to NPMJS:

  • The user name
  • password
  • Email address (this is the information you use when registering on the NPMJS website)

This step can be omitted if this is not your first connection.

You can also check your connection by running the following command.


npm who am i
Copy the code

If successful, you can proceed to the final step: publish

If you want to quit:


npm logout
Copy the code

Upload the package to NPM

NPM publish if successful:

npm notice npm notice package: [email protected] NPM notice === Tarball Contents === NPM notice 1.3kB index.js NPM notice 220B package.json NPM notice === Tarball Details === NPM notice name: Tool61 NPM notice Version: 1.0.0 NPM notice Package size: 855 B NPM Notice Unpacked Size: 1.5 kB NPM notice SHasum: 3bfba7bc92e242810a850ac39ded7ebe992a6d9c npm notice integrity: sha512-VoRuxxbcGzXen[…] v3tKWYUT1B1AQ== npm notice total files: 2 npm notice

What can go wrong is:

  • The package name has been used by someone else.
  • Incorrect package version number: each time a publish, the package version number should be greater than the previous one.
  • The file is too large. You may need to create.npmignoreFile to set which files to ignore when packaging. Here’s a demo.

# .npmignore
/node_modules
npm-debug.log
/src
/examples
/build
Copy the code

If there is no error, it means that everything is OK. You can log in NPM with your account password to check whether the package under your name has myNpm

Download the use

The NPM install package name is used to install the package. Then, tell your friends to download it.

Delete the package

NPM unpublish --force // Forcibly deleteCopy the code

If your package is useless, delete it to save public resources.

Update package

  1. Modify the code, save.
  2. Updated the version number. Changes can be made directly in package.json: only larger, not smaller.
  3. To publish

What format is the package?

Format:

  • Make sure you have package.json. It is used to illustrate the package as a whole. (Name, Version, Description, Author, dependencies,……) .npm init --yes
  • It has its own code file. This code file is declared in main in package.json.

Do we need to switch images before uploading to NPM?

Yes, we have to upload the code to the NPM website. Although we upload to NPM’s official website, it will automatically synchronize (for example, every 15 minutes through other images of the latest package information) to other images – Taobao, CNPM…..

Do I need to log in to upload?

Yes, log in from the command line. The NPM adduser operation only needs to be done once

Is there a contract on the package name when uploading?

There is. 1. Cannot have the same name as an existing package; 2. The package name must contain lowercase letters, digits, and hyphens

What is the upload command?

Is NPM publish

What are the possibilities of uploading errors?

  1. Your account has not been verified (you will receive an email in your registered email address and need to click confirm). It seems that QQ mailbox does not support operation in mobile phone, it is recommended to operate in PC browser.
  2. No login, no NPM adduser
  3. Poor Network environment

How do I version update a package?

  1. Update the code for rerelease
  2. Upgrade version (the upgrade version can only be larger, but cannot be smaller). Modify the version value in package.json
  3. npm publish

How to download the package

Command: NPM I Package name

  1. Download the package in another project
  2. To speed up the download, we will switch the image to Taobao.