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

yarn test (node --run test) fails in Node.js 22.12 due to requiring top-level await module #16930

Closed
tats-u opened this issue Dec 10, 2024 · 4 comments · Fixed by #16938
Closed

Comments

@tats-u
Copy link
Contributor

tats-u commented Dec 10, 2024

Environments:

  • Prettier Version: b40509c (main)
  • Running Prettier via: Node.js
  • Runtime: Node.js 22.12.0
  • Operating System: Windows
  • Prettier plugins (if any): N/A

Steps to reproduce:

node --version
git clone https://github.com/prettier/prettier.git
cd prettier
yarn
yarn test
node --run test

Expected behavior:

(Test succeeds twice)

Actual behavior:

v22.12.0
Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:392:13)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1414:24)
    at Module._compile (node:internal/modules/cjs/loader:1547:5)
    at Object..js (node:internal/modules/cjs/loader:1677:16)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)

git diff package.json
diff --git a/package.json b/package.json
index 49cdf6f39..7635ebe4a 100644
--- a/package.json
+++ b/package.json
@@ -158,7 +158,7 @@
   },
   "scripts": {
     "prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1",
-    "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
+    "test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-print-required-tla\" jest --runInBand",
     "test:dev-package": "cross-env INSTALL_PACKAGE=1 yarn test",
     "test:dist": "cross-env NODE_ENV=production yarn test",
     "test:dist-standalone": "cross-env TEST_STANDALONE=1 yarn test:dist",

Error: unexpected top-level await at file:///D:/prettier/node_modules/temp-dir/index.js:4
const temporaryDirectory = await fs.realpath(os.tmpdir());
                           ^



Error: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:395:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1414:24)
    at Module._compile (node:internal/modules/cjs/loader:1547:5)
    at Object..js (node:internal/modules/cjs/loader:1677:16)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
yarn why temp-dir
└─ tempy@npm:3.1.0
   └─ temp-dir@npm:3.0.0 (via npm:^3.0.0)
@tats-u
Copy link
Contributor Author

tats-u commented Dec 10, 2024

We have to await import("tempy") and propagate asyncness to callers.
It finally reaches to jest.config.js, but Jest seems to decline to read it.

@tats-u
Copy link
Contributor Author

tats-u commented Dec 10, 2024

This makes the test work again, but I don't want Prettier to set it because it's just a makeshift:

git diff package.json
diff --git a/package.json b/package.json
index 49cdf6f39..b082612f4 100644
--- a/package.json
+++ b/package.json
@@ -158,7 +158,7 @@
   },
   "scripts": {
     "prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1",
-    "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
+    "test": "cross-env NODE_OPTIONS=\"--no-experimental-require-module  --experimental-vm-modules\" jest --runInBand",
     "test:dev-package": "cross-env INSTALL_PACKAGE=1 yarn test",
     "test:dist": "cross-env NODE_ENV=production yarn test",
     "test:dist-standalone": "cross-env TEST_STANDALONE=1 yarn test:dist",
 PASS  tests/integration/__tests__/cache.js (29.593 s)
(node:25144) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:25144) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

↑ These look like culprits.

I think we need to make all test scripts modules.

@tats-u
Copy link
Contributor Author

tats-u commented Dec 10, 2024

22.12 introduced require(esm) without flag: nodejs/node#55217, but it looks like incompatible with the current test code of Prettier.

@tats-u tats-u changed the title yarn test (node --run test) fails in Node.js 22.12 yarn test (node --run test) fails in Node.js 22.12 due to requiring top-level await module Dec 10, 2024
@fisker
Copy link
Member

fisker commented Dec 10, 2024

jestjs/jest#15363

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

Successfully merging a pull request may close this issue.

2 participants