eslint-plugin-import-x
eslint-plugin-import-x copied to clipboard
`Unable to resolve path to module` on `bun` or modules with `bun:` prefix
Importing modules with bun: prefix (eg. bun:test) or bun itself with @types/bun installed results in a resolution error with import-x/no-unresolved
Unable to resolve path to module 'bun:test'. eslint import-x/no-unresolved
index.test.ts
import { expect, test } from 'bun:test';
test('2 + 2', () => {
expect(2 + 2).toBe(4);
});
Configuration Solution / Workaround
If automating this based on the TS types in @types/bun is not possible / feasible / wanted, then one way that I've found to work around this is to use the ignore option of import-x/no-unresolved :
eslint.config.js
const configArray = [
rules: {
'import-x/no-unresolved':
- 'error',
+ [
+ 'error',
+ {
+ ignore: ['^bun(:\\w+)?$'],
+ },
+ ],
},
},
];
Full config
import eslintTypescript from '@typescript-eslint/eslint-plugin';
import typescriptParser from '@typescript-eslint/parser';
import eslintImportX from 'eslint-plugin-import-x';
import globals from 'globals';
/** @type
* {import('@typescript-eslint/utils/ts-eslint').FlatConfig.ConfigArray}
* */
const configArray = [
{
// Lint common extensions by default with rules above
files: [
'**/*.js',
'**/*.jsx',
'**/*.cjs',
'**/*.mjs',
'**/*.ts',
'**/*.tsx',
'**/*.cts',
'**/*.mts',
],
languageOptions: {
parser: typescriptParser,
parserOptions: {
project: './tsconfig.json',
// typescript-eslint specific options
warnOnUnsupportedTypeScriptVersion: true,
},
globals: {
...globals.browser,
...globals.node,
...globals.commonjs,
...globals.es2021,
// Allow using React as a global without importing it
React: true,
},
},
plugins: {
'@typescript-eslint': {
rules: eslintTypescript.rules,
},
'import-x': eslintImportX,
},
settings: {
'import-x/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import-x/resolver': {
// Load <rootdir>/tsconfig.json
typescript: true,
node: true,
},
},
rules: {
// Error on imports that don't match the underlying file
// system
// https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/no-unresolved.md
'import-x/no-unresolved': [
'error',
{
ignore: ['^bun:\\w+$'],
},
],
},
},
];
export default configArray;
See also https://github.com/import-js/eslint-import-resolver-typescript/pull/266
import { isBuiltin } from 'node:module'
console.log(isBuiltin('bun:sqlite'))
// In Node.js: false
// In Bun: true
Thus we can't use isBuiltin here as ESLint might still be running under Node.js rather than Bun (E.g. in IDE).
Ah just saw this other PR:
- https://github.com/import-js/eslint-import-resolver-typescript/pull/288
I guess once this is published and eslint-plugin-import-x upgrades to it, it will resolve the issue
@karlhorky You don't have to upgrade eslint-plugin-import-x. You only need to upgrade eslint-import-resolver-typescript.
I just became the co-maintainer of eslint-import-resolver-typescript and I will release a new version of eslint-import-resolver-typescript as soon as possible.
The latest version of eslint-import-resolver-typescript (which was released about 15 hours ago) should have fixed the issue.
@SukkaW thanks for the heads up!
Yeah indeed, I can confirm that [email protected] resolves the issue 🎉
Relevant PR from @SunsetTechuila :
- https://github.com/import-js/eslint-import-resolver-typescript/pull/288
This uses the new is-bun-module package (GitHub), nice 👀
cc @Jarred-Sumner
Ah, just upgraded dependencies and received Unable to resolve path to module 'bun:test' from import-x/no-unresolved again:
$ yarn eslint . --max-warnings 0
$ /home/runner/work/courses/courses/node_modules/.bin/eslint . --max-warnings 0
/home/runner/work/courses/courses/packages/curriculum-versions/curriculumVersions/2024-3-fall-pern-extensive-eu/drone/__tests__/smokeTest.test.ts
Error: 2:30 error Unable to resolve path to module 'bun:test' import-x/no-unresolved
/home/runner/work/courses/courses/packages/curriculum-versions/curriculumVersions/2025-1-winter-pern-extensive-eu/drone/__tests__/smokeTest.test.ts
Error: 2:30 error Unable to resolve path to module 'bun:test' import-x/no-unresolved
/home/runner/work/courses/courses/packages/fusion.upleveled.io/__tests__/smokeTestDevServer.test.ts
Error: 6:30 error Unable to resolve path to module 'bun:test' import-x/no-unresolved
/home/runner/work/courses/courses/packages/learn.upleveled.io/__tests__/smokeTestDevServer.test.ts
Error: 6:30 error Unable to resolve path to module 'bun:test' import-x/no-unresolved
Looking into the last dependency changes, it appears that [email protected] caused this change:
Minor Changes
- https://github.com/import-js/eslint-import-resolver-typescript/pull/391 c8121e5 Thanks @JounQin! - feat: make is-bun-module as optional peer dependency
Technically this is a BREAKING CHANGE, but considering we just raise out v4 recently and this only affects bun users, bun --bun eslint even works without this dependency, so I'd consider this as a minor change.
So for bun users, there are three options:
- install
is-bun-moduledependency manually and usebun: trueoption- run
eslintwithbun --bun eslintw/obun: trueoption- enable
run#bunin bunfig.toml w/obun: trueoption
@JounQin @SukkaW should this be added to the readme of eslint-plugin-import-x?
@karlhorky Sorry for breaking, feel free to PR!
~~And considering https://github.com/import-js/eslint-import-resolver-typescript/pull/402 will be released soon,~~ some doc changes would be needed.
eslint-import-resolver-typescript v4.2.3 has just been released.
@JounQin thanks for the release!
I'll try to track down what changes are needed (maybe even code changes to eslint-plugin-import-x?
Some things which I tried so far which didn't work:
- Adding
is-bun-moduleas a dependency - Upgrading to
[email protected]
@karlhorky No change needed in eslint-plugin-import-x.
Did you enable bun: true option for eslint-import-resolver-typescript? Or just try bun --bun eslint?
Ah yeah, I forgot that I configured import-x/resolver in our shared config.
I'll take a look at:
- [x] Reconfiguring this in
eslint-config-upleveledhttps://github.com/upleveled/eslint-config-upleveled/commit/c01e0db0d0121344eb1bb5d8714fdbc811be2df1 - [x] Document
bunoption ineslint-import-resolver-typescripthttps://github.com/import-js/eslint-import-resolver-typescript/pull/404 - [x] Updating the
eslint-plugin-import-xreadme for theimport-x/resolverconfig https://github.com/un-ts/eslint-plugin-import-x/pull/262
@karlhorky I'd recommend using import-x/resolver-next when possible for flat config. See aslo https://github.com/import-js/eslint-import-resolver-typescript#eslintconfigjs
(This part is also missing in eslint-plugin-import-x's README itself unfortunately. 😂
I'd recommend using
import-x/resolver-nextwhen possible for flat config.
Nice, this looks great! I'll wait until it's not marked next anymore to upgrade.