我的包结构如下:
mypackage
|--- src
| |--- component1
| `--- component2
`--- dist
|--- component1
`--- component2
Run Code Online (Sandbox Code Playgroud)
当我将它发布到 npm 时,我希望它看起来像下面这样,没有dist
目录:
mypackage
|--- component1
`--- component2
Run Code Online (Sandbox Code Playgroud)
这个想法是,当我从这个包中导入时,导入应该是这样的:
import component1 from 'mypackage/component1'
Run Code Online (Sandbox Code Playgroud)
不是这个(注意额外的dist
):
import component1 from 'mypackage/dist/component1'
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?我目前在我的 package.json 中有一个文件部分,它发布了额外的内容dist
,我不希望这样:
"files": [
"dist/"
]
Run Code Online (Sandbox Code Playgroud) 我尝试实现使用 GitHub Actions 发布和安装包的官方指南:使用细化权限验证包注册表
失败:
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://npm.pkg.github.com
npm ERR! need auth You need to authorize this machine using `npm adduser`
Run Code Online (Sandbox Code Playgroud)
包.json
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://npm.pkg.github.com
npm ERR! need auth You need to authorize this machine using `npm adduser`
Run Code Online (Sandbox Code Playgroud)
.github/workflow/ci.yml
{
"name": "@charneykaye/banana",
"version": "4.0.6",
"repository": "git@github.com:charneykaye/banana",
"description": "made by …
Run Code Online (Sandbox Code Playgroud) node.js npm npm-publish github-actions github-package-registry
我刚刚创建了一个新程序包:https : //github.com/supericium/pli
我现在正尝试将其首次发布到NPM,如下所示:
ole@MKI:~/Sandbox/pli$ npm publish --access public
npm ERR! publish Failed PUT 404
npm ERR! Linux 3.13.0-93-generic
npm ERR! argv "/home/ole/.nvm/versions/v6.4.0/bin/node" "/home/ole/.nvm/versions/v6.4.0/bin/npm" "publish" "--access" "public"
npm ERR! node v6.4.0
npm ERR! npm v3.10.3
npm ERR! code E404
npm ERR! 404 Not found : @supericium/pli
npm ERR! 404
npm ERR! 404 '@supericium/pli' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm …
Run Code Online (Sandbox Code Playgroud) 我正在尝试发布我的模块以查看它是如何工作的。
我安装了 npm,然后创建了我的非常简单的模块。
我曾经npm add user
将自己添加为新用户。我收到以下消息:Logged in as david1994 on https://registry.npmjs.org/
然后我尝试使用 发布我的模块npm publish
,但出现以下错误:
npm ERR! code: 'EPERM',
npm ERR! syscall: 'open',
npm ERR! path: 'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\supermodule\\npm-debug.log.1866747129' }
Run Code Online (Sandbox Code Playgroud)
但是我npm-debug.log
在模块目录中看不到任何内容。
当我尝试在用作依赖项的模块中使用 React Hooks 时,它失败了。我收到以下错误:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
Run Code Online (Sandbox Code Playgroud)
但如果我使用基于类的组件,那就没问题了。为什么会发生这种情况?
这是我的摘录package.json
{
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "./node_modules/.bin/babel src -d dist",
"prepublish": "npm run build"
},
"peerDependencies": {
"react": "^16.10.2",
"react-router-dom": "^5.1.2"
},
"devDependencies": {
"axios": "^0.19.0",
"jwt-decode": "^2.2.0",
"qs": "^6.9.0",
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/preset-react": "^7.6.3"
} …
Run Code Online (Sandbox Code Playgroud) 我正在向 NPM 发布一个库。该库是用Typescript编写的,Typescript编译的结果放在一个lib/
文件夹中。主文件是index.ts
导出我的公共 API 的文件:
export * from './functions';
export * from './models';
Run Code Online (Sandbox Code Playgroud)
在 package.json 中,我指定:
{"main": "lib/index.js", "types": "lib/index.d.ts", "files": ["lib/**/*"]}
Run Code Online (Sandbox Code Playgroud)
一旦我的包发布并安装在其他地方,它的文件夹结构是:
lib/
index.js
index.d.ts
functions/
index.ts
my-function.ts
models/
index.ts
my-model.ts
Run Code Online (Sandbox Code Playgroud)
消费者从包根目录导入所有内容:
{"main": "lib/index.js", "types": "lib/index.d.ts", "files": ["lib/**/*"]}
Run Code Online (Sandbox Code Playgroud)
这可行,但我喜欢隔离路径的想法。一种用于函数,另一种用于模型。目前,如果我删除顶级索引,导入将被隔离,但它们现在包含lib/
我不想要的路径中的文件夹:
lib/
index.js
index.d.ts
functions/
index.ts
my-function.ts
models/
index.ts
my-model.ts
Run Code Online (Sandbox Code Playgroud)
我实际上想要完成的事情是相同的,但没有lib/
. 我有一个可行的解决方案,但它似乎太复杂了。这是有效的:
main
types
和files
键。index.ts
npm publish
使用以下步骤替换简单的内容:
/lib
文件夹中我创建了一个 TypeScript 库,已使用以下结构发布到 npm:
mobx-state-router
|--- package.json
|--- src
| `--- index.ts (TypeScript source)
`--- dist
|--- index.js (compiled JavaScript)
|--- index.js.map (sourcemap)
`--- index.d.ts (type definitions)
Run Code Online (Sandbox Code Playgroud)
然后,我通过添加依赖项在 React 应用程序中使用该库。在 Chrome 开发工具中调试我的 React 应用程序时,我想在我的库中添加断点。但是,Chrome 开发工具向我显示了源代码的编译版本,而不是原始的 TypeScript。我希望通过将 TypeScript 源代码和源映射包含在我的包中,Chrome 开发工具能够显示我的 TypeScript 文件。
假设我有一个包含按钮、输入字段和更复杂组件的组件库,我想通过 npm 分发它,以便我可以在许多不同的“父项目”中使用它,以获得相同的外观和感受我所有不同的应用程序。
假设组件库和所有“父项目”都是在 React 中构建的。它们也可能是用 Vue 或 Angular 或其他东西构建的,但我们还是用 React 吧
还可以说,我非常喜欢 Material-Design 的外观和感觉,因此在我的组件库中,我想在某些组件中使用 Material-ui,但不是全部。我仍然想包装 Material-ui 组件,以确保我的所有父项目都以相同的方式使用它们,并且它们可能是定制的等等。
还可以说,我希望用户能够将某些父项目中的主题更改为深色模式,或者可能会影响排版或其他内容。因此,组件的样式取决于所选主题,父项目也可能如此。
还可以说,由于我非常喜欢材料设计,因此我可能也想直接在父项目中使用它。但只是在一些项目中。
现在我的问题是如何从技术上组织不同的项目。
太长了;我想要一个依赖于另一个第三方库的组件库,并希望尽可能封装我的组件。什么是一个好的结构呢?
我尝试使用新的 NPM 注册表,它现在是免费 GitLab 版本的一部分。我正在尝试创建一个 NPM 包并将其发布到我们公司的 GitLab 实例上。尝试运行时npm publish
,进程退出并显示错误:
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://gitlab.myemployer.com/api/v4/projects/1873/packages/npm/@sqt-klu%2fodysseus-nn-core
npm ERR! 404
npm ERR! 404 '@sqt-klu/odysseus-nn-core@0.0.1' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
Run Code Online (Sandbox Code Playgroud)
通过 GitLab CI/CD …
npm-publish ×10
npm ×8
node.js ×6
reactjs ×3
typescript ×2
components ×1
gitlab ×1
gitlab-ci ×1
material-ui ×1
npm-pack ×1
package.json ×1
react-hooks ×1