Skip to content

Error when using fully-qualified ESM imports with TypeScript #26827

@nwalters512

Description

@nwalters512

Current behavior

I have a fully-ESM project. I have a file boolean.ts like the following:

export const TRUE = true;

I try to import a value into one of my Cypress test files with an import like the following:

import { TRUE } from '../lib/boolean.js';

This fails with the following error:

Module not found: Error: Can't resolve '../lib/boolean.js' in '/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/e2e'
Looked for and couldn't find the file at the following paths:
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.js]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.json]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.jsx]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.mjs]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.coffee]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.ts]
[/Users/nathan/git/cypress-esm-fully-qualified-import-repro/cypress/lib/boolean.js.tsx]

Desired behavior

Cypress should support TypeScript-style fully-qualified ESM imports where one can import a .ts file by using a .js extension. In the context of this particular example, import ... from './modules.js should import module.ts.

Test code to reproduce

https://github.com/nwalters512/cypress-esm-fully-qualified-import-repro. Ignore the fact that defining and importing a TRUE constant isn't a real-world scenario 😉

Cypress Version

12.12.0

Node version

18.12.0

Operating System

macOS 13.3.1

Debug Logs

N/A

Other

I believe webpack's resolve.extensionAlias will be useful here. The example from their docs is probably pretty close to what you want:

module.exports = {
  //...
  resolve: {
    extensionAlias: {
      '.js': ['.ts', '.js'],
      '.mjs': ['.mts', '.mjs'],
    },
  },
};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions