How to publish your own package to npm (or your own/company's private source

In our daily work, during the development process, there will always be some shared components or public methods. If you have to paste and copy each project every time, you will make mistakes. In order to reduce the development cost, you can separate it as An npm library is usually built in the company's private library, but we can use our own private source for testing

First, how to build your own private library

There are many tools for building private libraries. I used verdaccio
this time because it is free...
and it is a fool-like installation

Node

First, your local Node version is greater than or equal to 12

Install Verdaccio

Global installation

 npm install -g verdaccio

The installation is complete

verdaccio

Check if the installation is successful
and then visit
http://localhost:4873/

insert image description here

The foreground is enabled using verdacciothe command.
The background is enabled by using pm2 or other process protection tools, and the terminal shutdown does not affect

install pm2

npm install pm2 -g

After the installation is complete, pm2start usingverdaccio

使用 pm2 启动 verdaccio
 pm2 start verdaccio

# 或通过路径启动
$ pm2 start PATH-TO-GLOBAL-VERDACCIO/verdaccio

# 查看服务状态
$ pm2 status

# 查看 pm2 守护下的进程 verdaccio 的实时日志
$ pm2 show verdaccio 

In this way, we put verrdacio in the background and execute it.
insert image description here
If you want to stop this service

pm2 stop verdaccio //停止某个服务
pm2 stop all //停止所有服务
pm2 restart  //重启服务

insert image description here

use native library

verdaccioAfter the service is started, you can use nrmtools to switch the source address.
For details, please refer to the npm add source and switch source I wrote before.

Install nrm globally
npm install -g nrm
to view existing configurations
nrm ls

insert image description here
Add native library to nrm

# 添加配置项
nrm add localNpm http://localhost:4873

# 切换 npm 源至私库
nrm use localNpm

# 添加环境用户并根据提示输入用户名密码,为后续传包做准备 (重要)
npm adduser --registry http://localhost:4873/

Switch npm source to private library

nrm use localNpm
insert image description here

This use and then use nrm ls to view the existing configuration will add a localNpm

Add npm account

npm adduser --registry http://localhost:4873/
insert image description here

publish npm package

Create a new test folder

Then execute
npm init
insert image description here
the new package.json file in the project

write inserted content

Add a new index.js for testing
insert image description here

release

Make sure that your npm source is your own nrm ls to check the front * is the source currently in use

Enter under the project just now
npm login
and follow the prompts to enter your account password email

insert image description here

npm publish
insert image description here

At this time, there will be the package to be added when visiting
insert image description here

Or you can publish on npm

At this time, confirm that your local source is npm,
and then go to register account password email npm official website

insert image description here
Then npm login
npm publish

insert image description here
success! ! ! ! !

Guess you like

Origin blog.csdn.net/zm_miner/article/details/125805922