10

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

1 Answer 1

9

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";
Sign up to request clarification or add additional context in comments.

1 Comment

I have used path mapping to make my module accessible in a better looking manner: typescriptlang.org/docs/handbook/…. I know that this wasn't the original question but some people may like to use import * from 'my-module' instead of import * from '../../my-module' so I thought I'd mention that here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.