NX Error for Relative Imports within the Same Project
Asked Answered
P

2

7

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';
Prodigious answered 2/11, 2022 at 22:38 Comment(3)
it's highly recommended to keep that behavior. Why would you want absolute imports?Alisha
I set up lots of aliases for my imports. Find it easier for me to grok by having: import thing from '@proj/controllers/thing' instead of: import thing from '../../../controllers/thing'Prodigious
I agree with @Alisha and also would like to make IntelliJ Idea to make use of relative imports instead in module and alias in other modules. I am not sure if this is possible.Commensal
P
5

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 }
        ]
      }
    }
  ]
}
Prodigious answered 3/11, 2022 at 0:16 Comment(3)
This doesn't fix the issue. It's not a circular dependency. This just turns the error off.Sibyl
@Sibyl in the context of nx it's a circular dependency.Ibo
This worked for me. I tried to setup local path alias for each package in my nx project but does not work at all. i.e. import { myUtil } from '@/shared' This was the only working solution for using local package aliases in nxCohligan
P
1

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.

Puiia answered 8/12, 2022 at 16:44 Comment(1)
This doesn't fix the issue. It's not a circular dependency so there should be no need to do this.Sibyl

© 2022 - 2024 — McMap. All rights reserved.