Author: IPlayCodex Warehouse: Github, codePen Blog: Nugget, SegmentFault, Zhihu, Jianshushu, Blogpark, LeetCode Public account: FEZONE contact me: [email protected] Special statement: Original is not easy, unauthorized shall not be reproduced or copied this article, otherwise according to infringement treatment, if you need to reprint or open the public number white list can contact me, respect the original respect for intellectual property rights from me

1. Introduction

What NPM is here do not have to say more, I believe that there is no front-end do not know. I won’t repeat it here. There must be a lot of reusable libraries or components in our daily development, so it is necessary to remove them and reuse them, which can greatly reduce our workload. Here is a brief description of how to publish a package to NPM

2. The tool

First let’s set up some useful tools

  • nvm
    • managementnodeVersion of a tool, how to install and use self – search
  • nrm
    • It can be easily switchednpmSource of a tool. Because at homenpmThe official source often uses an unstable network connectioncnpmThere will be an inexplicable bug. Therefore, it is strongly recommended to use this tool to managenpmAnd this tool can configure a custom source address, convenient for us to build our own private NPM

The use of the above two tools is no longer described here, their search

3. Initialize an NPM package

3.1. Create a sample package

// step 1
cd yourDic

// step 2
mkdir myFristNpmPackage

// step 3
cd myFristNpmPackage

// step 4
npm init
Copy the code

3.2. npm init

{
    "name": "myFristNpmPackage"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": []."author": ""."license": "ISC"
}
Copy the code

The entry file is index.js, so let’s create a new one

3.3. The create index. Js

export default printHelloWorld = () = > {
    console.log("hello world!");
};
Copy the code

Write a little code, just to experience the process

4. Register an NPM account

Sign up for an NPM account

5. Release

The simplest project above, which we are going to publish to NPM, goes into the project directory:

5.1. Confirm whether it is an official source

NRM has been installed on it. Generally, we will switch to Taobao source or a private source built by ourselves during development. But when publishing, you need to switch to the official source.

// step 1: change to npm
nrm use npm

// Step 2: Log in and enter your account password
npm login

// Step 3: Check whether the package name is occupied. If not, release the package. If yes, replace the package name with a new one
npm search myFristNpmPackage

// Step 4: Publish
npm publish
Copy the code

6. Publish

This completes the distribution of a package, which is actually quite simple

7. How to use it

Once you’ve published the package, it’s easy to use it:

// install myFristNpmPackage
npm i myFristNpmPackage --save
Copy the code

This is the end of a simple NPM package, very simple ~

Final 8.

❤️ attention + likes + favorites + comments + forwarding ❤️ original is not easy, encourage the author to create better articles ~