如何发布npm包

2,315 阅读4分钟

发包到 npm 比较简单,一共两个步骤:

  1. 编写你的包
  2. 发包到 npm registry

首先,你需要有一个 npm 账号。如果还没有可以访问这里创建一个。

其次,要通过命令行登陆 npm 账号(执行这一步骤之前首先要安装 Node.js 和 npm)。

npm login

此时会提示你输入用户名,密码和邮箱地址:

PS D:\Apache24\htdocs> npm login
npm notice Log in on https://registry.npmjs.org/
Username: rainyjune
Password:
Email: (this IS public) xxxxxx
Logged in as rainyjune on https://registry.npmjs.org/.

提示:如果你的 npm registry 修改过则会登录失败。

xxxxxxxx large-number % npm login     
npm notice Log in on https://registry.npm.taobao.org/
Username: chelfannie
Password: 
Password: 
Email: (this IS public) xxxxxxx@163.com
npm ERR! code E500
npm ERR! 500 Internal Server Error - PUT https://registry.npm.taobao.org/-/user/org.couchdb.user:chelfannie

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/dengqingfang/.npm/_logs/2021-11-10T03_30_50_178Z-debug.log

可以尝试如下命令修改默认的:

npm config set registry https://registry.npmjs.org/

修改之后,需要再次登录验证:

xxxxxx large-number % npm config set registry https://registry.npmjs.org/
xxxxxx large-number % npm login                                          
npm notice Log in on https://registry.npmjs.org/
Username: chelfannie
Password: 
Email: (this IS public) xxx@163.com
Logged in as chelfannie on https://registry.npmjs.org/.

image.png

第三步,你需要编写包了。 可以创建一个文件夹用于保存文件:

# Creating a folder named how-to-publish-to-npm
mkdir how-to-publish-to-npm

# Navigating into the folder
cd how-to-publish-to-npm

接下来,调用 npm init 命令完成包的初始化:

npm init

执行后它会询问你一些问题并最终在文件夹中创建一个 package.json 文件。此文件包含了要发包所需的基本信息。

package name: (how-to-publish-to-npm)
version: (1.0.0)
description: package description
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

最后一步是使用 npm publish 命令发布包。

npm publish

如果同名的包已经存在于 npm 则不能成功发布。会看到错误提示信息:

PS D:\Apache24\htdocs\how-to-publish-to-npm> npm publish
npm notice 
npm notice    how-to-publish-to-npm@1.0.0
npm notice === Tarball Contents ===
npm notice 236B package.json
npm notice === Tarball Details ===
npm notice name:          how-to-publish-to-npm
npm notice version:       1.0.0
npm notice filename:      how-to-publish-to-npm-1.0.0.tgz
npm notice package size:  252 B
npm notice unpacked size: 236 B
npm notice shasum:        25dd13d9c6101fb9057965e1d6cc8b6f7a7fabb9
npm notice integrity:     sha512-7vpYHg2hjWA6k[...]AAPbZnz2QtoIg==
npm notice total files:   1
npm notice
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

此时你需要修改包名,可以在 package.json 文件中修改 name 属性值。

{
  "name": "how-to-publish-to-npm-test",
  "version": "1.0.0",
  "description": "package description",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "MIT"
}

提示:你可以使用 npm search 命令搜索一个名字是否占用:

PS D:\Apache24\htdocs\test> npm search how-to-publish-to-npm
NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS
how-to-publish-to-npm     |                      | =jumpalottahigh | 2019-01-29 | 0.0.1    |

再次尝试 publish 命令。此时应该会看到成功信息。

PS D:\Apache24\htdocs\how-to-publish-to-npm> npm publish
npm notice
npm notice    how-to-publish-to-npm-kang@1.0.0
npm notice === Tarball Contents ===
npm notice 241B package.json
npm notice === Tarball Details ===
npm notice name:          how-to-publish-to-npm-kang
npm notice version:       1.0.0
npm notice filename:      how-to-publish-to-npm-kang-1.0.0.tgz
npm notice package size:  256 B
npm notice unpacked size: 241 B
npm notice shasum:        b58dfa879c039c9bf6918d6a0f2196b04220bee4
npm notice integrity:     sha512-c0aZNelWwncdm[...]5trKHQheSYFQA==
npm notice total files:   1
npm notice
+ how-to-publish-to-npm-kang@1.0.0

image.png

提示:如果你的 npm 账号的邮箱尚未验证通过,也会发包失败。需要先激活后再次尝试。 错误提示:

image.png

提示:如果你的 npm 账号未登录成功,也会发包失败。
错误提示:

xxxxxxxxx large-number % npm publish   
npm notice 
npm notice 📦  large-number@1.0.0
npm notice === Tarball Contents === 
npm notice 2.7kB dist/large-number.js    
npm notice 818B  dist/large-number.min.js
npm notice 233B  index.js                
npm notice 521B  package.json            
npm notice 564B  src/index.js            
npm notice 664B  webpack.config.js       
npm notice === Tarball Details === 
npm notice name:          large-number                            
npm notice version:       1.0.0                                   
npm notice filename:      large-number-1.0.0.tgz                  
npm notice package size:  2.2 kB                                  
npm notice unpacked size: 5.5 kB                                  
npm notice shasum:        17bfb93e78e76208ea78cd29b2c7c4e3250b0b81
npm notice integrity:     sha512-Ey6LD2PLALYMJ[...]PZd1z+WcNhG9g==
npm notice total files:   6                                       
npm notice 
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/dengqingfang/.npm/_logs/2021-11-10T03_31_50_710Z-debug.log

如果包名被占用了怎么办?

这是一个常见问题,因为很多人都发包到 npm 上。有时候很难得到你想要的包名(类似于抢注 .com 域名)。

为了解决这个问题,npm 允许发包到一个命名空间下。这意味着可以把包发布到你自己的用户名或者组织名下,因此解决了命名问题。

要发包到一个命名空间下,你需要选择如下其中一种方式:

  • 手动修改 package.json 中的 name 属性值为 @username/package-name 格式
  • 运行 npm init --scope=username 而非 npm init

如果你的仓库有命名空间,你需要微调一下 publish 命令:

npm publish --access public

至此我们已经了解了如何发包到 npm。

参考文档: 如何发布npm包