都2024年了,试试发个npm包吧

156 阅读3分钟

关于源的切换

执行 npm whoami​ 后如果提示 You need to authorize this machine using npm adduser​,此时需要给改机器配置登录权限。

此时需要先将 npm ​源切换至中国科技大学官方源:

https://registry.npmmirror.com  淘宝
https://npm.aliyun.com 阿里云
https://mirrors.cloud.tencent.com/npm/ 腾讯
https://npmreg.proxy.ustclug.org  中科大

备注:尝试试了几下,中科大、npm官方可以完成npm adduser​、npm public​操作,其他源(淘宝、腾讯、华为)则不行

设置源,到发布步骤如下:

# 查看当前设备登录用户名
npm whoami

# 设置源为官方的或者中科大的
npm config set registry https://npmreg.proxy.ustclug.org (中科大)
npm config set registry=http://registry.npmjs.org (官方)

# 查看当前设备的源
npm config get registry

# 绑定账号
npm adduser 或者 npm login

# 执行发布
npm public

# 查看是否发布成功

npm search [bun_name]

如果使用了npm​源管理工具nrm​,则可以更方便的切换源,使用方法如下:

# 安装nrm
npm install -g nrm

# 查看可使用的源
nrm ls

# 切换源
nrm use taobao
nrm use npm (切回官方源)

# 测试速度
nrm test

‍‍```html
# 安装nrm
npm install -g nrm

# 查看可使用的源
nrm ls

# 切换源
nrm use taobao
nrm use npm (切回官方源)

# 测试速度
nrm test

‍‍```

此时执行npm adduser​,顺利的话,即可登录成功,期间会让你回车到浏览器中认证,登录认证后关闭浏览器即可。

npm notice Log in on https://registry.npmjs.org/
Create your account at:
https://www.npmjs.com/login?next=/login/cli/90f1a64d-688e-45ea-854e-97c6c8fcadcb
Press ENTER to open in the browser...

Logged in on https://registry.npmjs.org/.

执行发包(npm publish​)

npm的发包也会有一堆的坑,主要有以下几点:

1、无反应,因为国内链接npm官网不稳定,所以会一直卡死在access阶段,此时等待或者多试几次即可。

npm notice
npm notice 📦  @tct_efcs/components@0.1.0
npm notice Tarball Contents
npm notice 0B README.md
npm notice 1.2kB dist/if/index.js
npm notice 1.2kB dist/index.js
npm notice 4.4kB dist/select-advance/demo/select-all.js
npm notice 4.5kB dist/select-advance/demo/select-label-name.js
npm notice 3.7kB dist/select-advance/index.js
npm notice 664B package.json
npm notice Tarball Details
npm notice name: @tct_efcs/components
npm notice version: 0.1.0
npm notice filename: tct_efcs-components-0.1.0.tgz
npm notice package size: 3.3 kB
npm notice unpacked size: 15.6 kB
npm notice shasum: 3aa13c1d23455c6d4600139f270cb03e6576ad0e
npm notice integrity: sha512-Qo97qTRJ/66X6[...]wGatqVAge8KGg==
npm notice total files: 7
npm notice
npm notice Publishing to https://registry.npmjs.org/ with tag latest and public access

2、404 - Note that you can also install from a npm error 404 tarball, folder, http url, or git url.

这种情况下一般会同时报下面的错误信息

npm error code E404
npm error 404 Not Found - PUT https://registry.npmjs.org/@tct_efcs%2fcomponents - Scope not found
npm error 404
npm error 404  '@tct_efcs/components@0.1.0' is not in this registry.

这种情况下一般有三种可能,分别如下:

1、你的账号权限受限,不让你提交。此时一般是由于你删库,24小时内无法再次推送新包

2、你的电脑的access token​失效,需要重新执行npm adduser

3、你尝试发布的npm包的作用域名称不存在,一般带有@符号的包名通常指的是作用域包,需要提前在npm进行注册,然后才能进行发布操作