javascript - 发布到 npm, `main` 中定义的文件未发布

标签 javascript node.js typescript npm

我正在尝试将包发布到 npm 并且出于某种原因我在 package.json 中定义了 .js不包含在包装中。我的项目是用 typescript 编写的,我通过以下 npm 脚本进行转译...

"prepublishOnly": "tsc -p ./ --outDir dist/"

它正确地将我期望的文件输出到本地 dist/app.js。然后我执行 npm publish 并发布了 readme.md 和一些其他文件,但不是我期望的 app.jspackage.json 的主要设置为 dist/app.js 那么我在这里缺少什么?

这是我的第一个 npm 包发布,所以它可能是愚蠢的,很乐意尝试任何想法。

最佳答案

这个问题很可能是因为你有一个 .gitignore 文件,如果没有 .npmignore,npm 会将其视为后备。找到文件。我猜您遇到了上述情况,也许忽略了源代码管理中的 /dist

If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file

main实际上不会决定打包什么内容......

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

此外,打包文件可以通过 files 显式包含

"files": ["dist/app.js", "/and/others/"]

最后,如果您看一下 npm/issues/3571 ,您可以通过简单地添加一个空的 .npmignore 文件来解决这个问题。

If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it.


专业提示 - 您可以运行 npm pack在本地,它将执行发布的所有生命周期 Hook ,并为您提供 .tgz 包,就好像它是从 npm 获取的一样。您可以解压缩它以检查内容。我发现这在很多情况下都非常有用,可以在不实际执行的情况下模拟发布。

关于javascript - 发布到 npm, `main` 中定义的文件未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47339444/

相关文章:

javascript - NodeJS REPL 变量赋值——需要帮助找出问题所在

node.js - 指定草稿订单货币 Shopify

angular - 下拉列表不会填充 Angular 中的 API 数据

visual-studio-2012 - 构建错误: The command "tsc --comments --module AMD --sourcemap --target ES3 " exited with code 1

javascript - 无法为自定义 Angular 组件捕获 nativeElement

javascript - 从javascript中给定的字符串索引中删除n个字符

node.js - Node JS 使用自定义 header 调用 GRPC 服务器

typescript - 类型 'connect' 上不存在属性 'Observable<any>' | RXJS多播

JavaScript 照片 slider

java - 如何从 GET 请求中获取参数?