Rémi Rousselet
Rémi Rousselet

Reputation: 277677

tsc is trying to resolve relative path modules in the wrong folder

I'm using npm link to reference a typescript library I'm developing in my test project

Which means that my node_modules looks like this :

node_modules/
|   myLib/
|   |   dist/
|   |   |   subModule/
|   |   |   |   index.js
|   |   |   |   index.d.ts
|   |   |   index.js
|   |   |   index.d.ts
|   |   node_modules/
|   |   src/
|   |   tsconfig.json
|   |   package.json

Which implies that when I'm trying to reference my library using import X from "myLib" I have to tell the compiler that the sources are in the /dist forlder, not that the root of myLib.

I solved this by adding a "main": "./dist/index.js" in the package.json of myLib

The problem is when I try to import a path relative to myLib Like import Y from "myLib/subModule"

This time it doesn't work. Because instead of looking at node_modules/myLib/dist/subModule tsc is looking at node_modules/myLib/subModules/dist/ which doesn't exist.

How can I make the compiler to look at the right path for subModules ?

Upvotes: 2

Views: 2808

Answers (1)

Chris Krycho
Chris Krycho

Reputation: 3155

You can resolve this by using the "paths" key in the "compilerOptions" in your tsconfig.json. Something like this:

{
  "compilerOptions": {
    "paths": {
      "myLib/*": "node_modules/myLib/dist/*"
    }
  }
}

Sadly, this is something of a standing issue with how the TypeScript compiler resolves definition files when you have a "types" key in your package.json.

Upvotes: 1

Related Questions