3
votes

I created a monorepo with lerna (the code in github).

Inside I have two packages each package export sum function.

In app1 folder I want to import this function but vscode doesn't find the function.

Not sure why. I set everything correctly, and vscode should be offer from two paths:

@packages/pck1
@packages/pck2

/tsconfig.json

{
  "extends": "./tsconfig.build.json",

  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@packages/*": ["packages/*/src"]
    },
    "jsx": "react",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "types": []
  }
}

If you can clone my repo - run npm install then open vscode and go to apps/app1/src/index.ts and write sum. wait for typescript to find the function. you will get:

enter image description here

can't find the reason, any help will be appreciated.

1

1 Answers

0
votes

If you are using scoped packages, they would be called @pck1 and @pck2, not @packages/SOMETHING as your top-level tsconfig.json suggests.

I could get your example working as follows:

Add these compilerOptions to apps/app1/tsconfig.json:

"baseUrl": ".",
"paths": {
  "@pck1": ["../../packages/pck1/src"]
}

And change apps/app1/src/index.ts:

import { sum } from '@pck1';
sum(9,9);

Your basic mistake is: You consider packages part of the packages' names but it is just a directory. I have created a PR on github for this: https://github.com/wizardnet972/lerna-test/pull/1

I have a similar lerna test project on github: https://github.com/gflohr/lerna-deps. It is explained in a blog post http://www.guido-flohr.net/lerna-mono-repos-with-internal-dependencies/. Maybe you can use that as a starting point.