工作中开发了个npm包,无法在.npmrc中单独配置仓库路径,需要添加scope,特记录解决问题的方法。
1、什么是Scope,即安装包时,使用的@someScope/packageName,如@antv/g6
2、按照正常命令发布npm包,只是在执行npm init命令时增加参数:
npm init --scope=<<your-scope-name>>
//例如 npm init --scope=test
或者执行npm init命令,包名参数进行改动(如下命令中的包名、路径等均为示例,非实际路径):
PS D:\code> npm init
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: (mypackage) @test/mypackage //包名改为scope+包名
version: (1.0.0)
description:
entry point: (mypackage.common.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\code\mypackage\package.json:
{
"name": "@test/mypackage",
"version": "1.0.0",
"description": "",
"main": "mypackage.common.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
这样即可为npm包添加上scope