“Cannot find module” error when using TypeScript 3 Project References

谁说我不能喝 提交于 2020-01-23 11:09:18

问题


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

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

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

Here's utils.ts:

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

Here's Shared/tsconfig.json:

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

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

Here's 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" }]
}

Here's the problem file - person.ts:

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

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

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

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


回答1:


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";


来源:https://stackoverflow.com/questions/52283465/cannot-find-module-error-when-using-typescript-3-project-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!