foreword
In our daily work, we may need to download packages, create packages, publish packages, and so on. This tweet will record operations on npm packages in daily projects.
reference package
We can use the npm install command to reference and download the packages exposed by the npm warehouse.
The business public components we develop need to be publicly referenced in the company's internal projects, and we don't want to be publicly referenced by outsiders. At this time we need to deploy a private NPM repository. Such as Alibaba Cloud's npm private warehouse.
Packages that cannot be fetched from private warehouses can also be fetched from domestic mirror servers (such as Taobao) or npm public servers by setting up agents.
Administrators can configure user permissions and npm packages in the warehouse through the interface. This is not much. Let's talk about how we should pull the public package of the project from the private warehouse in our daily work.
step one:
The private warehouse actually tampers with the original link to the NPM source warehouse as a private warehouse link. So we first need to set the npm default warehouse
npm config set registry=https://packages.aliyun.com/xxxxxxxx/npm/npm-registry/
Step two:
Log in to the npm account assigned by the private warehouse
npm login
Next, we can go back to our project and execute npm ci or i. In this way, the reference package file in the project will be pulled from the private warehouse
create package
Step 1: Log in to the npm account (you can register on the npm official website )
If you have already logged in your own npm account and want to switch the account of the project contract, you can also use login to switch the number
npm login
Step 2: Go to your package directory
cd npmDemo
Step 3: Initialize
npm init
Fill in the relevant project information.
Note that the package name here is your package name after the upload review is completed, which can be found on the npm official website.
You can also use this package name for npm i installation
npm i npm_demo_v_v
release package
Execute the publish command in the package folder:
npm publish
Notes on distribution packages
Some projects usually use domestic mirror warehouses, such as Taobao. At this time, you must first modify the default npm warehouse source,
Because the Taobao mirror source is a read-only source, you need to go to the npm source to publish npm. If you don’t modify it, you will get an error
nrm use npm
# 如果没有安装nrm可以使用原生
npm config set registry=http://registry.npmjs.org
If it is the first time to send a package, you need to execute:
npm adduser
[You need to add an npm account, and enter the name, password, email, and one-time login password as required]
off the shelf bag
Can be removed after 24 hours
npm --force unpublish npm_demo_v_v
update package
Modify the code according to your own logic, and finally modify the version in package.json.
npm version patch //相当于是先本地更新然后上传包
npm publish