6

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';
S. C.
  • 127
  • 1
  • 6
  • it's highly recommended to keep that behavior. Why would you want absolute imports? – Juri Nov 03 '22 at 20:19
  • 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'` – S. C. Dec 13 '22 at 14:33
  • I agree with @Juri 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. – Felix Jan 17 '23 at 10:24

2 Answers2

1

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 }
        ]
      }
    }
  ]
}
S. C.
  • 127
  • 1
  • 6
0

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.

Nevin Madhukar K
  • 3,031
  • 2
  • 25
  • 52
  • This doesn't fix the issue. It's not a circular dependency so there should be no need to do this. – nomad Jun 01 '23 at 00:26