【问题标题】:Call JS package as npm script into another package将 JS 包作为 npm 脚本调用到另一个包中
【发布时间】:2022-01-25 09:14:15
【问题描述】:

我有一个依赖于Package1的包Package2

套餐 1: 由以下文件组成(本例)

      package.json
      index.js
      /bin/package1 (no extension)

包 1 package.json:

      ...
      {
      "name": "package1",
      "version": "0.0.1",
      "description": "a JavaScript library that should be called by other stuff",
      "main": "index.js",
      "scripts": {
        "bin": "cli.js"
      },
      ...

包 1 /bin/package1 脚本:


      #!/usr/bin/env node
      const package1 = require('../'); 
      // just import the package1 module an it should run  without function call

包 2 是依赖于包 1 的包。

包 2 package.json:

      ...
      "name": "package2",
      "version": "1.0.0",
      "description": "a JavaScript library that depends on package1",
      "main": "index.js",
      "scripts": {
        "serve" : "package1 ."
      },
      ...

但是当我运行 yarn serve 时出现错误:

yarn run v1.22.17
$ package1 .
'package1' is not recognized as an internal or external command,
operable program or batch file.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我尝试了很多不同的东西,但我无法让它发挥作用。

【问题讨论】:

  • 您是否尝试将 .js 扩展名添加到 /bin/package1
  • 是的,我的初始测试是使用 .js 扩展名,在检查了按预期工作的其他包的源代码后,他们没有使用它

标签: javascript node.js


【解决方案1】:

您需要在 package 1 的 package.json 中添加“scripts”标签之后:


    ...
    "scripts": {....},
    "bin": {
        "package1": "bin/package1"
      },
    ...

就是这样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多