S. C.
S. C.

Reputation: 327

NX Error for Relative Imports within the Same Project

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';

Upvotes: 7

Views: 7164

Answers (2)

Nevin
Nevin

Reputation: 3298

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.

Upvotes: 2

S. C.
S. C.

Reputation: 327

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 }
        ]
      }
    }
  ]
}

Upvotes: 6

Related Questions