NX Error for Relative Imports within the Same Project

4.5k Views Asked by At

I'm getting an error when using TS aliased paths within the same project: Projects should use relative imports to import from other files within the same project

I don't want this behavior. Any idea how to disable?

I tried playing with the @nrwl/nx/enforce-module-boundaries option, but it has almost no documentation around its options

// NX doesn't like this line which uses a path to a file within the
// same NX project. It wants me to use relative pathing, which I
// don't want to use
import { fooHandler } from '@handlers/foo';
2

There are 2 best solutions below

1
On

For those who are coming here without this getting resolved. (nx monorepo usage)

For lint error:

Projects should use relative imports to import from other files within the same project - eslint rule @nrwl/nx/enforce-module-boundaries fails

  1. Add "allowCircularSelfDependency": true.
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "allowCircularSelfDependency": true, -> This may solve the lint error.
            "allow": ["@account/**"], -> // White list the lint error.
             ...
          }
  1. Whitelist the folders: Add "allow": [@foldername]
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            
            "allow": ["@account/**"], -> // White list the lint error.
             ...
           }

That should fix it.

3
On

Had to look through the npm package, but found it by searching for the error text. You can disable it like this from inside of your .eslintrc.json settings:

{
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          // This is the part you need to add
          { "allowCircularSelfDependency": true }
        ]
      }
    }
  ]
}