Search code examples
javascriptnpmes6-modulesmonorepolerna

How to shield the internals of a Lerna package, so other packages cannot import them?


I am working with a Lerna monorepo. I often see one package make imports from deep inside another package, for example:

import { SomeType } from "@schema/folder/folder/file.ts"

This is undesirable, because sometimes we need to transform auto-generated types before exporting them via index.d.ts. This sometimes results in the wrong, non-transformed type being imported from deep inside the package.

I would like to somehow constrain the files/folders that one package exposes to the others, so such imports would not be possible:

import { SomeType } from "@schema" // valid
import { SomeType2 } from "@schema/folder/folder/file.ts" // invalid

What are my options?

I am not sure what other info I can provide since I have little to no experience with this stuff.


Solution

  • When you use eslint, you can add a no-restricted-imports rule to the eslint settings of the package:

        "no-restricted-imports": [
          "error",
          {
            "patterns": ["@schema/folder/*"]
          }
        ],