How can I remove parent path when importing a package from same monorepo?

147 Views Asked by At

I have a nodejs project which uses nx to manage multiple packages, the structure is:

packages/my-proj1/src/*.ts
packages/my-proj2/src/*.ts
...

the my-prod2 has dependency on my-proj1, so in the package.json under my-proj2, it has dependency:

"dependencies": {
  "my-proj1": "*",
}

in the code of my-proj2, it imports files from my-proj1 like:

import { ... } from 'my-proj1/src`

as you can see, it has to put src on the import statement. How can I make it work by removing the src folder from the import statement in my-proj2?

1

There are 1 best solutions below

1
Alex Wayne On

You should have a tsconfig.base.json file in the root of the monorepo where these paths are setup.

All you have to is change the my-proj1 path to include the src directory.

paths: {
  "my-proj1": "path/to/my-proj1/src"
}

Though it's usually recommended to have a single file that exports everything from a project, and you'd then reference just that file:

// path/to/my-proj1/src/index.ts

export * from 'lib/somefile'
export * from 'lib/otherfile'

Then on the tasconfig.base.json:

paths: {
  "my-proj1": "path/to/my-proj1/src/index.ts"
}