npm私有仓库
# npm 仓库使用
# npm公有仓库
# 创建npm账号
npm 创建账户使用文档 (opens new window)
# 初始化项目发布
1、本地创建一个文件夹:例如:harmony-sdk
2、cd harmony-sdk 执行npm init, 默认即可
harmony-sdk git:(main) ✗ npm init --scope=@zclearn
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 json` 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: (@zclearn/harmony-sdk)
version: (1.0.1)
git repository:
author:
license: (ISC)
About to write to /Users/zhangjinchao/Documents/kk-self/self-project/hub-notes/npm/harmony-sdk/package.json:
{
"name": "@zclearn/harmony-sdk",
"version": "1.0.1",
"description": "harmony webview sdk",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"keywords": [
"harmony",
"mini"
],
"license": "ISC"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
3、在harmony-sdk文件夹中创建一个文件名为index.js的文件,简单的写了一下内容。
(function(w) {
ENV();
let harmony = {};
harmony.navigateTo = function({ url = ''} = {}) {
if(!url) {
console.warn('url parameter error!')
return false;
}
return callMethods({methodName: 'navigateTo', params: {url}})
}
harmony.navigateBack = function() {
return callMethods({methodName: 'navigateBack', params: {}})
}
harmony.redirectTo = function({ url = "" } = {}) {
if(!url) {
console.warn('url parameter error!')
return false;
}
return callMethods({methodName: 'redirectTo', params: {url}})
}
w.harmony = harmony;
function ENV() {
if(!w.JsCallbackToHarmony) {
console.warn('Please open in Harmony App!');
return false;
} return true;
}
function callMethods({methodName = "", params = {}} = {}) {
if (!ENV()) throw new Error('ENV Error');
const data = {
name: methodName,
data: params
}
const dataString = JSON.stringify(data)
return w.JsCallbackToHarmony.call(dataString)
}
})(window);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
4、本机第一次发布包(非第一次可忽略)
提示
切换源为npm,且检查源地址为https://registry.npmjs.org/
非第一次发包,使用npm login命令
修改源地址为官方源:
npm config set registry https://registry.npmjs.org/
1
提示
推荐使用nrm: npm install -g nrm
添加registry地址: nrm add taobao https://registry.npm.taobao.org
nrm ls 查询当前源 nrm use 切换源
* npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
taobao ----- https://registry.npm.taobao.org/
nj --------- https://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
1
2
3
4
5
6
7
2
3
4
5
6
7
npm adduser
Username: zclearn
Password:
Email: (this IS public) zjc_it2013@163.com
Authenticator provided OTP: *******
1
2
3
4
5
2
3
4
5
5、npm publish 发包
npm publish
1
# nexus私服仓库
# 登录私服仓库
注意
确定源已切换至私有仓库的源
注意方式二在根目录下.npmrc会产生相应的环境变量,避免冲突,登录仓库报401时注意检查
a、方式一
$ npm login --registry "http://nexus.quickcan.com/nexus/content/repositories/npm-internal/"
Username: ****
Password:
Email: (this IS public) ******@kkworld.com
Logged in as zhnagjinchao on http://nexus.quickcan.com/nexus/content/repositories/npm-internal/.
1
2
3
4
5
2
3
4
5
b、方式二
# 生成凭证、user和password分别替换为你的账户和密码(ldap账号)
zhangjinchao@myhost kk-tool $ echo -n 'user:password' | openssl base64
dXNlcjpwYXNzd29yZA==
zhangjinchao@myhost kk-tool $
# 编辑 .npmrc 文件,写入以下内容。【注意】email 处写你的邮箱地址 。_auth 值填写第一步生成的值。
# vi .npmrc
email=you@example.com
always-auth=true
_auth=YWRtaW46YWRtaW4xMjM=
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 发布包到私服仓库
a、加上 registry 参数,执行发布命令
npm publish --registry http://nexus.quickcan.com/nexus/content/repositories/npm-internal/
1
b、package.json 文件里增加 publishConfig 配置
"publishConfig" : {
"registry" : "http://nexus.quickcan.com/nexus/content/repositories/npm-internal/"
},
npm publish
1
2
3
4
5
6
2
3
4
5
6
# Other
1、nrm常用命令
nrm //展示nrm可用命令
nrm ls //列出已经配置的所有仓库
nrm test //测试所有仓库的响应时间
nrm add <registryName> <registryUrl> //新增仓库
nrm use <registry> //切换仓库
nrm rename <oldName> <newName> // 重命名
nrm del <registryName> // 删除源
1
2
3
4
5
6
7
2
3
4
5
6
7
2、已发布npm包删除、废弃
npm unpublish <pkg>[@<version>] // 删除已发布某个版本
npm deprecate <pkg>[@<version>] <message> // 废弃某个版本并给出相应提示信息
1
2
2
3、npm包发布分为有范围和无范围,私有仓库需要收费
# 替换组织名称或用户范围名称
npm init --scope=@zclearn
# npm publish 发布出现 publish Failed PUT 402
npm publish --access public
1
2
3
4
5
2
3
4
5
# 问题
1、登录时报如下错误,请检查源地址是否为https
原因:2021年10月4日后,npm 网站和 npm registry 必须使用 TLS 安全套接层 1.2 版本,所以需要升级对应版本
npm ERR! code E426
npm ERR! 426 Upgrade Required - PUT http://registry.npmjs.org/-/user/org.couchdb.user:zclearn
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/zhangjinchao/.npm/_logs/2022-04-13T07_47_18_334Z-debug.log
1
2
3
4
5
2
3
4
5
编辑 (opens new window)
上次更新: 2024/06/26, 16:54:02