npm - `npm publish` 忽略 .npmignore 的新增内容

标签 npm npm-publish npmignore npm-pack

构建create-near-app时,会在项目树中创建工件。其中一些 were added.npmignore 文件,但其他文件被遗漏了,直到我尝试发布新版本并注意到由于 Rust 构建工件,npm 包将有数百兆字节大(!!)。我added这些到 .npmignore,但 npmpublish 没有注意到。

我可以通过删除文件来解决这个问题,但是对于开源库来说,这种解决方法很容易出错,而开源库可能由不断壮大和不断变化的贡献者团队维护和发布。

npm 是否将 .npmignore 文件缓存在某处?如何让 npm 使用新设置?


详细信息和快速引用:

操作系统:macOS 10.15.6
节点版本管理器:asdf v0.7.6
节点:v12.14.1
npm:v6.13.7

package.json:

-- snip --
"files": [
  "/common",
  "/templates"
],
-- snip --

.npmignore:

common/contracts/rust/target
common/contracts/out
templates/*/.cache
templates/*/assembly
-- snip --

npm pack --dryrun 的输出:

-- snip --
npm notice 293.2kB common/contracts/rust/target/debug/build/ryu-140fa0ad757694f9/build_script_build-140fa0ad757694f9                                                                           
npm notice 20.6kB  common/contracts/rust/target/debug/build/ryu-140fa0ad757694f9/build_script_build-140fa0ad757694f9.dSYM/Contents/Resources/DWARF/build_script_build-140fa0ad757694f9         
npm notice 297.4kB common/contracts/rust/target/debug/build/serde-3bef95b74f70906a/build_script_build-3bef95b74f70906a                                                                         
-- snip --

如果你想自己尝试一下:

  1. 克隆存储库
  2. cd common/contracts/rust
  3. cargo 构建(您需要install Rust)
  4. cd ../../..
  5. npm pack --dryrun

这对你有用吗?这是“仅在我的机器上损坏”的事情吗?

最佳答案

"Is this an "only broken on my machine" thing?"

不,根据您的项目当前配置,这(几乎)是预期的行为 - 并且 npm 不会缓存 .npmignore 文件某处。

我之前说“几乎”的原因是因为你在上一条评论中说:

"So in this case, all paths in templates/* are ignored, but the paths in common/contracts are included in the package."

情况不应该是这样,因为根据文档中所述的 npm 预期行为,(请参阅下面的“为什么会发生这种情况?”部分),当前在您的 <鉴于您当前的 package.jsonfiles 配置,em>.npmignore 文件应该有效。

您遇到的是奇怪行为,即某些模式受到尊重,而其他模式则没有,但是希望通过遵循两种建议的解决方案/方法(如下)中的任何一种,您将获得正确的期望结果。


为什么会发生这种情况?

package.json 的文档 files字段指出与您的问题相关的以下内容:

注意:粗体强调是我添加的。

You can also provide a .npmignore file in the root of your package or in subdirectories, which will keep files from being included. At the root of your package it will not override the “files” field, but in subdirectories it will.

因此,当前在您的 .npmignore 中指定模式的原因 文件被忽略是因为它们试图覆盖当前在package.json文件中指定的包含模式 field 。本质上,由于您的 .npmignore 文件当前位于包的根目录,因此它不会覆盖"file"字段


解决方案

这里有两种不同的解决方案/方法可供考虑(排名不分先后):

  1. 省略 package.json 中的 files 条目,并根据需要扩充当前的 .npmignore 文件

    即从 package.json 中删除此内容:

    "files": [
     "/common",
     "/templates"
    ],

    Then create additional patterns in your current .npmignore file for any other directories/files that should also be excluded from packing/publishing. In other words add any patterns to .npmignore for assets that do not reside somewhere in /common or /templates that should also be excluded.

    For example, after briefly looking at your repo, you'll need to add the pattern tests to .npmignore too.

    In summary, this first approach is a case of delete the files field and specify all patterns for assets to be ignored in one place - in the .npmignore file at the root of your project.

  2. Or, keep the files entry in package.json as-is and create additional .npmignore files in subdirectories

    Given the patterns currently specified in your .npmignore file are:

    common/contracts/rust/target
    common/contracts/out
    templates/*/.cache
    templates/*/assembly
    templates/*/contract
    templates/*/package.json
    templates/*/src/assets
    templates/*/src/config.js
    templates/*/src/global.css
    templates/*/src/utils.js
    

    and also given that the docs stated; "at the root of your package [.npmignore] will not override the “files” field, but in subdirectories it will" then you'll need to:

    1. Create a .npmignore file in the common/contracts/ directory containing the following patterns:

      rust/target
      out
      
    2. templates/ 目录中创建一个包含以下模式的 .npmignore 文件:

      */.cache
      */assembly
      */contract
      */package.json
      */src/assets
      */src/config.js
      */src/global.css
      */src/utils.js
      

    使用第二种方法,当前驻留在项目目录根目录(按原样)的 .npmignore 文件变得多余。


注意:希望通过遵循上述两种解决方案/方法(严格遵守 npm 记录的期望),您将获得所需的结果。但是,如果这些提议的解决方案都没有产生所需的结果,那么我建议将提议的解决方案之一保留为基线起点,然后阅读 this SO thread 中的一些答案。 。是的,这个问题与您的问题相反,但是您可能会进一步了解后续步骤,特别是从this answer - 它提到“npm pack 并发布尊重目录树中的任何 package.json 文件,而不仅仅是根目录中的文件。”。


关于npm - `npm publish` 忽略 .npmignore 的新增内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63046027/

相关文章:

node.js - npm install 任务启动时间太长

javascript - 无法导入包以 react 样板

azure - 在azure devops管道中执行gulp脚本

typescript - 无论我尝试什么,npm 都不会忽略我的 TypeScript 源代码文件

npm - 如何从 npmjs 获取最新版本的包

javascript - 在 Webpack 中获取 "Cannot resolve module ' aws-sdk' ,'child_process' ,'net' "in/node_modules/watchpack

git - .npmignore 和 .gitignore 有什么区别?

node.js - package-lock.json 也应该发布吗?

git - .npmignore 扩展/继承自 .gitignore

azure - Azure DevOps NPM 注册表中生产/测试版的最佳实践