Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot resolve imports with subpaths #3076

Open
OnkelTem opened this issue Sep 30, 2024 · 5 comments
Open

Cannot resolve imports with subpaths #3076

OnkelTem opened this issue Sep 30, 2024 · 5 comments

Comments

@OnkelTem
Copy link

OnkelTem commented Sep 30, 2024

I cannot get it working with any subpath imports, e.g.

some-file.tsx:

import { item } from 'package/subpath'

leads to an error:

ESLint: Unable to resolve path to module 'package/subpath'.(import/no-unresolved)

At the same time, TypeScript works and doesn't find any issues.

ESLint config:

.eslintrc.js:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
  },
  settings: {
    'import/extensions': ['.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs'],
    'import/resolver': {
      typescript: true,
    },
  },
  plugins: ['@typescript-eslint', 'react-hooks', 'only-warn'],
  extends: ['airbnb', 'plugin:@typescript-eslint/recommended', 'plugin:react-hooks/recommended', 'prettier'],

This is a part of package.json of package:

  "exports": {
    "require": "./dist/cjs/index.js",
    "import": "./dist/mjs/index.js",
    "default": "./dist/mjs/index.js"
  },
  "main": "./dist/cjs/index.js",
  "module": "./dist/mjs/index.js",
  "types": "./dist/types/index.d.ts",

ESLint: 8.x

@ljharb
Copy link
Member

ljharb commented Oct 3, 2024

That's because we don't support exports yet - since resolve also doesn't. When resolve does, so will we.

Separately, your exports field there HAS no subpaths, so nothing should resolve a subpath anyways.

@zdm
Copy link

zdm commented Oct 5, 2024

Just a question - I get similar error, but related to package imports:

import/no-unresolved, Unable to resolve path to module '#lib/result'.

Seems that package imports also not supported by the node resolver?

@ljharb
Copy link
Member

ljharb commented Oct 5, 2024

@zdm yes, imports uses and depends on exports.

@zdm
Copy link

zdm commented Oct 5, 2024

Webpack uses https://github.com/webpack/enhanced-resolve.
It highly configurable and fully supports all possible features.
Maybe it can replace current node resolver.
(And current webpack resolver too)

@zdm
Copy link

zdm commented Oct 5, 2024

Or use built-in possibilities:
This will work for node projects, and will work better than handmade resolver.

const { createRequire } = require( "node:module" );
const url = require( "node:url" );

function resolve ( name, from ) {
    try {
        return createRequire( url.pathToFileURL( from ) ).resolve( name );
    }
    catch {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants