每天一点小进步--npm包的创建,发布
1.首先注册一个npm账号2.创建目录,通过npm init命令生成package.json文件name 发布到npm之后对应的包名称 npm-testversion 目前发布包的版本号description描述entry point 入口文件 默认index.jstest command 测试命令git repository 仓库地址keyword 关键字 帮助别人更快的找到你的包author
·
1.首先注册一个npm账号
2.创建目录,通过npm init命令生成package.json文件
- name 发布到npm之后对应的包名称 npm-test
- version 目前发布包的版本号
- description 描述
- entry point 入口文件 默认index.js
- test command 测试命令
- git repository 仓库地址
- keyword 关键字 帮助别人更快的找到你的包
- author 作者
3.创建index.js文件
// index.js
export function num (num1,num2) {
return num1 + num2
}
4.通过命令行登陆npm npm login
根据提示输入用户名,密码,邮箱。 邮箱需要验证完毕
5.切换npm源
npm config set registry http://registry.npmjs.org
6.发布
npm publish
7.安装使用
npm install 包名
// 引用
import {num} from 'npm-test' // or const npmTest = require('npm-test')
num(2,4)
npmTest.num(4,5)
8.更新
手动修改package.json中的版本号 然后npm publish
9.撤销发布
由于撤销发布会让把要撤销的包作为依赖的包不能正常工作,所以npm官方对包的撤销是有限制的:
- 不允许撤销发布已经超过24小时的包(
unpublish is only allowed with versions published in the last 24 hours
) - 如果在24小时内确实要撤销,需要加--force参数
- 即使撤销了发布的包,再次发布的时候也不能与之前被撤销的包的名称/版本其中之一相同,因为这两者构成的唯一性已经被占用,官方并没有随着撤销而删除
撤销发布的命令为 npm unpublish
npm unpublish npm-test
// 报错
npm ERR! Refusing to delete entire project.
npm ERR! Run with --force to do this.
npm ERR! npm unpublish [<@scope>/]<pkg>[@<version>]
// 加 --force参数重新撤销发布
npm unpublish npm-test --force
npm WARN using --force I sure hope you know what you are doing.
- my-test-project
10.常见报错
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT http://registry.npmjs.org/npm-test - You do not have permission to publish "npm-test". Are you logged in as the correct user?
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.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\MyPC\AppData\Roaming\npm-cache\_logs\2020-12-22T03_28_48_055Z-debug.log
这个报错是包名重复 修改一个包名即可
code E403 没权限发布
npm ERR! [no_perms] Private mode enable, only admin can
publish this module [no_perms]
Private mode enable, only admin can publish
this module: your-package
这个报错是因为你把源设置为第三方源了,改回来就可以 使用5中的命令
更多推荐
已为社区贡献1条内容
所有评论(0)