通过 npm 链接和 typescript 本地开发的多模块

bab*_*itt 5 node.js npm typescript tsc

我正在尝试使用 NPM 将依赖模块 B 与其依赖项 A 链接起来。以此指导,我完成了以下操作。

项目A定义为:

{
  "name": "typescript-link-failure-a",
  "version": "1.0.0",
  "description": "",
  "main": "dist/A.js",
  "typescript": "dist/A.d.ts",
  "scripts": {
    "setup": "npm link"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-typescript": "^2.13.6",
    "merge2": "^1.0.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

A.ts

export class A {
    "foo":string;
}
Run Code Online (Sandbox Code Playgroud)

npm run-script setup执行以创建从此目录到全局 node_modules 的链接。

在项目B中:

{
  "name": "typescript-link-failure-b",
  "version": "1.0.0",
  "description": "",
  "main": "dist/B.js",
  "scripts": {
    "preinstall": "npm link ../A"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-typescript": "^2.13.6",
    "merge2": "^1.0.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

B.ts

import {A} from "typescript-link-failure-a"

class B {
    "foo":A;
}
Run Code Online (Sandbox Code Playgroud)

我使用 NPM 链接链接回项目 A。我认为链接正常,就像在项目 B 的 node_modules 中一样,我可以看到项目 A。

然而,当我尝试

import {A} from "typescript-link-failure-a"、Intellij、VS Code 和 TSC 都失败,表示它们不理解名为 typescript-link-failure-a 的模块。

任何人都可以提供见解吗?

github 代码示例链接

bab*_*itt 4

在这种情况下,项目 B 需要引用项目 A 的名称而不是路径来执行 NPM 链接。

将其更改为“npm link typescript-link-failure-a”作为项目 B 根目录中的命令解决了问题。