如何发布一个NPM包
在我们写代码的时候总是使用别人的npm包,然而我有时心底会好奇自己如何发布一个npm包呢,什么时候自己的包能够被很多人喜欢并使用呢...今天我终于迈出了第一步
·
如何发布一个NPM包
1,编写模块
作为演示实例,新建一个sayHi.js文件,相关代码如下:
exports.sayHi = function(){
return 'Hello World';
}
2,初始化包
package.json文件的内容尽管相对较多,,但实际发布一个包时并不需要一行一行的编写。使用 npm init 命令(在终端执行)就能帮你生成package.json文件。(NPM会通过提问式的交互逐个填入选项就行)
> npm init
//然后根据提示操作就行了
//执行结果
npm WARN config production Use `--omit=dev` instead.
npm WARN config production Use `--omit=dev` instead.
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (a01) a01
version: (1.0.0) 0.0.1
description: A hello world package
git repository:
keywords: Hello World
author: smzk
license: (ISC)
About to write to E:\webqBasic\package.json:
{
"name": "a01",
"version": "0.0.1",
"description": "A hello world package",
"main": "know.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "smzk",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {},
"keywords": [
"Hello",
"World"
]
}
Is this OK? (yes)
3,注册包仓库账号
为了维护包,NPM必须要使用仓库账号才允许将包发布到仓库中。注册账号的命令是 npm adduser 。(也是提问式的交互)
> npm adduser
//然后根据提示操作就行了
//执行结果
npm WARN config production Use `--omit=dev` instead.
npm WARN config production Use `--omit=dev` instead.
npm WARN adduser `adduser` will be split into `login` and `register` in a future version. `adduser` will become an alias of `register`. `login` (currently an alias) will
become its own command.
npm notice Log in on https://registry.npmjs.org/
Username: smzk
Password:
Email: (this IS public) 2717141437@qq.com
npm notice Please check your email for a one-time password (OTP)
Enter one-time password: 60185323
Logged in as smzk on https://registry.npmjs.org/.
注意:可能注册的时候会报下面的错误([FORBIDDEN] Public registration is not allowed)
npm WARN config production Use `--omit=dev` instead.
npm WARN adduser `adduser` will be split into `login` and `register` in a future version. `adduser` will become an alias of `register`. `login` (currently an alias) will
become its own command.
npm notice Log in on https://registry.npmmirror.com/
Username: smzk
Password:
Email: (this IS public) 2717141437@qq.com
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmmirror.com/-/user/org.couchdb.user:smzk - [FORBIDDEN] Public registration is not allowed
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.
npm ERR! A complete log of this run can be found in:
问题原因:是你的NPM使用了镜像地址。
解决方法:输入命令 npm config set registry https://registry.npmjs.org/ 即可
查看npm源:
> npm config get registry
切换npm源方法一:
> npm config set registry http://registry.npmjs.org
切换npm源方法二:
> nrm use npm
4,上传包
上传包的命令 npm publish ,
> npm publish .
//执行结果
npm WARN config production Use `--omit=dev` instead.
npm notice
npm notice 📦 a01@0.0.1
npm notice === Tarball Contents ===
npm notice 254B know.js
npm notice 345B package.json
npm notice === Tarball Details ===
npm notice name: a01
npm notice version: 0.0.1
npm notice filename: a01-0.0.1.tgz
npm notice package size: 464 B
npm notice unpacked size: 599 B
npm notice shasum: 22f555f7736aaa790dc9292d0f8e4a07d5219100
npm notice integrity: sha512-DLR4vVCfYQcPp[...]9RyTObjzLfOow==
npm notice total files: 2
npm notice
npm notice Publishing to https://registry.npmjs.org/
+ a01@0.0.1
5,安装包
为了测试自己上传的包,可以换一个目录执行命令 npm install a01 (a01 是包名)
> npm install a01
//执行结果
npm WARN config production Use `--omit=dev` instead.
npm WARN config production Use `--omit=dev` instead.
added 58 packages, and audited 59 packages in 13s
7 packages are looking for funding
run `npm fund` for details
6,管理包权限
如果需要多人对包进行发布,可以使用命令 npm owner 帮助你
//查看管理包的所有者
> npm owner ls a01
//执行结果
npm WARN config production Use `--omit=dev` instead.
npm WARN config production Use `--omit=dev` instead.
smzk <2717141437@qq.com>
//添加包的拥有者
> npm owner add <user> <package name>
//删除包的拥有者
> npm owner rm <user> <package name>
7,拓展(分析包)
在使用NPM过程种,当你不能确定当前目录下能否可以通过require()顺利引入想要的包时,可以执行命令 npm ls 分析包。
这个命令可以分析出当前路径下能够通过模块路径找到的所有包,并生成依赖树。
├── connect-mongo@4.6.0
├── cookie-parser@1.4.6
├── debug@2.6.9
├── ejs@2.6.2
├── express-session@1.17.3
├── express@4.16.4
├── http-errors@1.6.3
├── jsonwebtoken@8.5.1
├── mongoose@6.6.6
├── morgan@1.9.1
└── multer@1.4.5-lts.1
更多推荐
已为社区贡献1条内容
所有评论(0)