Skip to content

Commit f8aed28

Browse files
committed
fix: make sure esm configs are indeed loaded as ESM
1 parent e69a05b commit f8aed28

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ _Released 9/23/2025 (PENDING)_
66
**Bugfixes:**
77

88
- In development mode, Electron `stderr` is piped directly to Cypress' `stderr` to make it clear why Electron failed to start, if it fails to start. Fixes [#32358](https://github.com/cypress-io/cypress/issues/32358). Addressed in [32468](https://github.com/cypress-io/cypress/pull/32468).
9+
- Fixed an issue where ESM Cypress configurations were not being interpreted correctly. Fixes [#32493](https://github.com/cypress-io/cypress/issues/32493). Fixed in [#32515](https://github.com/cypress-io/cypress/pull/32515).
910

1011
**Misc:**
1112

packages/server/lib/plugins/child/run_require_async_child.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,37 @@ function run (ipc, file, projectRoot) {
8383
// Config file loading of modules is tested within
8484
// system-tests/projects/config-cjs-and-esm/*
8585
const loadFile = async (file) => {
86+
let errorLoadingCJSConfig = null
87+
8688
try {
8789
debug('Loading file %s', file)
8890

8991
return require(file)
9092
} catch (err) {
9193
if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
92-
throw err
94+
errorLoadingCJSConfig = err
9395
}
9496
}
9597

96-
debug('User is loading an ESM config file')
98+
debug('User may be trying to load an ESM config file')
9799

98100
try {
99101
// We cannot replace the initial `require` with `await import` because
100102
// Certain modules cannot be dynamically imported.
101103
// pathToFileURL for windows interop: https://github.com/nodejs/node/issues/31710
102104
const fileURL = pathToFileURL(file).href
103105

104-
debug(`importing esm file %s`, fileURL)
106+
debug(`importing config as esm file %s`, fileURL)
107+
const config = await import(fileURL)
105108

106-
return await import(fileURL)
109+
return config
107110
} catch (err) {
108111
debug('error loading file via native Node.js module loader %s', err.message)
112+
if (errorLoadingCJSConfig) {
113+
debug('CJS loading initially failed. Rethrowing %s', errorLoadingCJSConfig.message)
114+
throw errorLoadingCJSConfig
115+
}
116+
109117
throw err
110118
}
111119
}

system-tests/test/issue_32493_spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ describe('e2e issue 32493: ESM config loading should not fail', () => {
44
systemTests.setup()
55

66
systemTests.it('loads the config file', {
7+
spec: 'app.cy.js',
8+
browser: 'chrome',
79
project: 'config-cjs-and-esm/config-with-mjs',
810
expectedExitCode: 0,
911
})

0 commit comments

Comments
 (0)