Npm 从 repo 安装而不是 运行 `prepare`

Npm install from repo not running `prepare`

我有一个 npm 包,用于托管在内部 git 服务器上的通用组件。出于某种原因,当我在另一个项目中调用 npm install 时,我想在其中使用它不会 运行 prepare 挂钩。显然,这不起作用,因为 npm 包需要 node_modules 中的 /dist 文件夹才能使用该包。

我已经尝试过诸如使用已弃用的 prepublish 钩子之类的方法,但即使这样也不会被调用。我还尝试 postinstall 看看我是否可以在安装后构建,而那个钩子确实被调用它失败了,因为没有安装 devDependencies

package.json

{
  "name": "common-components",
  "version": "0.1.0",
  "scripts": {
    "prepare": "npm run build",
    "build": "ng build",
    ...
  },
  "private": true,
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
}

用于安装的命令

npm install --save git+ssh://{URL-to-common-components-repo}}

我已经通读了 npm-scripts 文档https://docs.npmjs.com/misc/scripts,他们似乎坚持认为 prepare 钩子应该始终被调用用于这个确切的用例

2019 年 5 月 6 日更新

请注意,我在 NPM 社区 https://npm.community/t/using-npm-ci-does-not-run-prepare-script-for-git-modules/632/4.

上发现了这个错误

我正在使用 npm 6.4.1,根据错误应该可以正常工作

对于那些想知道这个状态的人。我无法让它工作。我最终做的是将组件托管在私有 npm 注册表上并且工作正常,因为 npm publish 命令将执行构建并且只发布 dist 文件夹

最近要检查的一件事是我遇到了一个包 - 如果有 .gitignore 而不是 .npmignore npm 可能会忽略你的 /dist 文件夹。添加一个空的 .npmignore 在这种情况下有效。

"If there’s no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it."

来自 https://docs.npmjs.com/misc/developers

在 package.json 中添加 main 为我解决了这个问题。

    "main": "./dist/index.js",
    "scripts": {
       "build": "babel src --out-dir dist",
       "prepare": "npm run build",
       "lint": "eslint ."
     },

node v14.15.4 npm 6.14.11

如果添加空 .npmignore 没有帮助,您可以尝试在 package.json#files 中明确指定 dist 中的所有文件。如果这可行,您可能需要考虑使用与 dist 中的文件匹配的通配符模式来简化 package.json.

package.json

    ...
    "files": [
        "source",
        "dist/cjs/main.js",
        "dist/es/main.js"
    ]
}

在 npm/cli 存储库 https://github.com/npm/cli/issues/1287#issuecomment-635021757

中查看对类似问题的评论