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/
The foreground is enabled using verdaccio
the 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, pm2
start 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.
If you want to stop this service
pm2 stop verdaccio //停止某个服务
pm2 stop all //停止所有服务
pm2 restart //重启服务
use native library
verdaccio
After the service is started, you can use nrm
tools 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
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
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/
publish npm package
Create a new test folder
Then execute
npm init
the new package.json file in the project
write inserted content
Add a new index.js for testing
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
npm publish
At this time, there will be the package to be added when visiting
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
Then npm login
npm publish
success! ! ! ! !