NPM 工作区 Typescript 无法找到本地模块

2024-01-05

我使用 NPM 工作区和 Typescript 设置了 NodeJS monorepo。它在没有 Typescript 的情况下工作得很好,但是引入 TS 会带来一些依赖解决错误。当前目录设置:

+-- node_modules
+-- package.json
+-- tsconfig.json
+-- tsconfig.build.json
+-- packages
    +-- core
    |   +-- package.json
    |   +-- tsconfig.json
    |   +-- src
        |   +-- index.ts
    +-- shared
    |   +-- package.json
    |   +-- tsconfig.json
    |   +-- src
        |   +-- helper.ts

Running npm ls确认所有内容都已正确符号链接:

[email protected] /cdn-cgi/l/email-protection C:\Users\<user>\Documents\Temp\monorepoDemo
├─┬ @my-packages/[email protected] /cdn-cgi/l/email-protection -> .\packages\core
│ └── @my-packages/[email protected] /cdn-cgi/l/email-protection deduped -> .\packages\shared
└── @my-packages/[email protected] /cdn-cgi/l/email-protection -> .\packages\shared

我有一个正在导出的虚拟辅助函数helper.ts in shared。正在导入哪个index.ts in core。根package.json, tsconfig.ts and tsconfig.build.json:

{
    "name": "monorepoDemo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "node packages/core/src/index.ts",
        "test": "echo \"Error: no test specified\" && exit 1",
        "build": "tsc -b --verbose tsconfig.build.json"
    },
    "author": "",
    "license": "ISC",
    "workspaces": [
        "packages\\core",
        "packages\\shared"
    ]
}
{
    "compilerOptions": {
        "composite": true,
        "target": "es2021",
        "module": "commonjs",
        "declaration": true,
        "declarationMap": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true
    }
}
{
    "files": [],
    "references": [
      {
          "path": "packages/core"
      },
      {
          "path": "packages/shared"
      }
    ]
}

The package.json and tsconfig.json of core:

{
  "name": "@my-packages/core",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@my-packages/shared": "^1.0.0"
  }
}
{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
      "rootDir": "./src",
      "outDir": "./dist",
      "baseUrl": ".",
      "paths": {
        "@my-packages/shared": ["../shared"]
        }
    },
    "references": [{ "path": "../shared" }],
    "exclude": ["dist"]
}

The package.json and tsconfig.json of shared:

{
  "name": "@my-packages/shared",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
      "rootDir": "./src",
      "outDir": "./dist"
    },
    "exclude": ["dist"]
}

以及源代码index.ts and helper.ts:

import { helper } from "@my-packages/shared"

(async () => {
    console.log("Bootstrapping from core package")

    helper()
})()
export function helper(): void {
    console.log("Running from shared package")
}

我遇到的错误index.ts: Cannot find module '@my-packages/shared' or its corresponding type declarations.ts(2307)

我认为这与缺少声明文件有关,因此尝试创建一个helper.ts但没有到达任何地方


The main现场package.jsonfile 是其他包(例如core) 导入包:

import { helper } from "@my-packages/shared"

这段代码将尝试寻找@my-packages/shared/index.js,它不存在。

当您构建共享包时,构建的文件位于dist/helper.js.

将主字段更改为"main": "dist/helper.js"让它发挥作用。如果您使用的是 vscode,请不要忘记重新启动语言服务器。

对于其他注意事项,例如特定的编译器选项或 esm 支持,请参阅文档Node.js 中的 ECMAScript 模块 https://www.typescriptlang.org/docs/handbook/esm-node.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NPM 工作区 Typescript 无法找到本地模块 的相关文章

随机推荐