import/no-relative-packages fix breaks in monorepo

See original GitHub issue

After updating my eslint config and running it on a yarn workspaces monorepo, I found that a new rule was added: import/no-relative-packages. I like the rule as it prevents someone from relatively importing something from another package.

But in my case, I have some test utils in a folder at the root of the monorepo which is just a folder and not a namespaced package. I’m currently importing them (relatively) only from some Jest tests. I’m not sure how the fix works, but in this case it seems to be simply assigning the name of the root folder for the project as the absolute path base like:

../../../testConfig/utils (import path before)

npm-library/testConfig/utils (after the fix)

The problem is it doesn’t work since Node can’t resolve ‘npm-library’. Perhaps it does work when fixing a relative import from a namespaced package but I haven’t tried.

If there’s not way to resolve this automatically, maybe it be a good idea to add an exception to the configuration (like a regex/glob pattern to disable for certain files/directories).

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Aug 29, 2022

Thanks, I’ll keep it open, this seems like a pretty important bug to fix.

1reaction
unleashitcommented, Aug 29, 2022

Thanks for the quick reply! npm-library (not the most creative name I admit) is actually the name of the monorepo, so that’s the top parent. It’s also the name the fix used as the parent folder for the absolute pathname.

So npm-library doesn’t live in the monorepo, it is the monorepo. The packages/apps are in a packages directory, and the tests are within the individual packages. Right under the parent (npm-library) and as a sibling to the package directory is a folder that contains the test configs, which I want to link to from the tests.

This could perhaps be a WSL2 issue because it doesn’t always handle symlinks correctly. But a lot of people are on WSL2.

If this isn’t enough info, it’ll be at least a month from now, but at that point I’ll have time to post a repro. So feel free to keep open or closed as you think is best 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Monorepo Import Problem
If the NgModule was imported via a relative import, then it's within the user's program and not node_modules , and a relative import...
Read more >
Configuring TypeScript for multiple packages in a Lerna + ...
In this article I will be showing you how to leverage the power of Lerna and yarn workspaces in a TypeScript project to...
Read more >
How to enable absolute path aliases with eslint import ...
Overwriting relative paths with paths absolute to the project will not change the relative relation of the files to each other. Instead, it...
Read more >
A Guide to Monorepos for Front-end Code
Only one package.json. No need to re-install dependencies in each repo whenever you want to update your dependencies. Re-use code with shared packages...
Read more >
eslint-plugin-import
This change log adheres to standards from Keep a CHANGELOG. Unreleased · 2.26.0 - 2022-04-05. Added. [ no-named-default , no ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found