Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot find module" error when using TypeScript 3 Project References

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

enter image description here

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

like image 618
Carl Rippon Avatar asked Sep 11 '18 20:09

Carl Rippon


People also ask

Can t find module typescript?

To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command. Copied!

How to fix Cannot find module or its corresponding Type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

What Is A typescript project reference?

Project references are a new feature in TypeScript 3.0 that allow you to structure your TypeScript programs into smaller pieces. By doing this, you can greatly improve build times, enforce logical separation between components, and organize your code in new and better ways.

Can not find module angular error?

To solve the error "Cannot find module '@angular/core'", make sure you have installed all dependencies by running the npm install command, set the baseUrl option to src in your tsconfig. json file and restart your IDE and development server. Copied!


1 Answers

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";
like image 97
Matt McCutchen Avatar answered Sep 27 '22 22:09

Matt McCutchen