“找不到模块"使用TypeScript 3项目参考时发生错误 [英] "Cannot find module" error when using TypeScript 3 Project References

查看:487
本文介绍了“找不到模块"使用TypeScript 3项目参考时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使 TypeScript 3的项目引用正常工作,但是很难从引用的项目中导入函数.

I'm trying to get TypeScript 3's project references working but struggling to import a function from a referenced project.

我有一个引用了 Shared ProjectA .这是文件结构:

I have a ProjectA that references Shared. Here's the file structure:

ProjectA
|_ src
|     |_person.ts
|_ tsconfig.json
|
Shared
|_ src
      |_utils.ts
|_ tsconfig.json

这是 utils.ts :

export function guid() {
  return "a guid";
}

这是 Shared/tsconfig.json :

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,

    "target": "es5",
    "outDir": "dist",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitReturns": true,
    "noImplicitAny": true
  },
  "include": ["src/**/*"]
}

这是 ProjectA/tsconfig.json :

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,

    "target": "es5",
    "outDir": "dist",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitReturns": true,
    "noImplicitAny": true
  },
  "include": ["src/**/*"],
  "references": [{ "path": "../shared" }]
}

这是问题文件- person.ts :

import { guid } from "../../Shared";

class Person {
  id: string;
  name: string;
  constructor() {
    this.id = guid();
  }
}

TS编译器出现错误,无法找到模块'../../Shared'"

The TS compiler errors with "Cannot find module '../../Shared'"

尝试导入 guid 函数时我在做什么错?任何帮助将不胜感激

What am I doing wrong when trying to import the guid function? Any help would be appreciated

推荐答案

您需要使用要从中导入文件的完整相对路径,就像文件在同一项目中一样:

You need to use the full relative path to the file you are importing from, just as you would if the file were in the same project:

import { guid } from "../../Shared/src/utils";

这篇关于“找不到模块"使用TypeScript 3项目参考时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆