-
Notifications
You must be signed in to change notification settings - Fork 31.3k
module: improve error message for top-level await in CommonJS #55874
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
base: main
Are you sure you want to change the base?
Changes from all commits
c9531ef
9dfd63d
1b637d6
04e570f
1e12966
11d788e
2f8e7d4
bd58f77
2e77c7c
42387e9
34e2c3c
5040118
8e720b1
ef625f3
bf87e8d
f9d84b2
b1f54e3
94f5a35
9099572
b76fdb4
c3a13fb
bb74388
01b4b6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,9 +246,12 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |
'await Promise.resolve(); console.log("executed");', | ||
]); | ||
|
||
strictEqual(stderr, ''); | ||
strictEqual(stdout, 'executed\n'); | ||
strictEqual(code, 0); | ||
match( | ||
stderr, | ||
/Top-level await is not supported in CommonJS modules\. To use top-level await, add "type": "module" to your package\.json or rename the file to use the \.mjs extension/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is titled “permits top-level |
||
); | ||
strictEqual(stdout, ''); | ||
strictEqual(code, 1); | ||
strictEqual(signal, null); | ||
}); | ||
|
||
|
@@ -258,9 +261,12 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |
fixtures.path('es-modules/tla/unresolved.js'), | ||
]); | ||
|
||
strictEqual(stderr, ''); | ||
match( | ||
stderr, | ||
/SyntaxError: Top-level await is not supported in CommonJS modules\. To use top-level await, add "type": "module" to your package\.json or rename the file to use the \.mjs extension/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is titled “reports unfinished top-level |
||
); | ||
strictEqual(stdout, ''); | ||
strictEqual(code, 13); | ||
strictEqual(code, 1); | ||
strictEqual(signal, null); | ||
}); | ||
|
||
|
@@ -270,9 +276,12 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |
'await Promise.resolve(); import "node:os"; console.log("executed");', | ||
]); | ||
|
||
strictEqual(stderr, ''); | ||
strictEqual(stdout, 'executed\n'); | ||
strictEqual(code, 0); | ||
match( | ||
stderr, | ||
/Top-level await is not supported in CommonJS modules\. To use top-level await, add "type": "module" to your package\.json or rename the file to use the \.mjs extension/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is titled “permits top-level |
||
); | ||
strictEqual(stdout, ''); | ||
strictEqual(code, 1); | ||
strictEqual(signal, null); | ||
}); | ||
|
||
|
@@ -282,7 +291,10 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |
'function fn() { await Promise.resolve(); } fn();', | ||
]); | ||
|
||
match(stderr, /SyntaxError: await is only valid in async function/); | ||
match( | ||
stderr, | ||
/Top-level await is not supported in CommonJS modules/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is titled “still throws on |
||
); | ||
strictEqual(stdout, ''); | ||
strictEqual(code, 1); | ||
strictEqual(signal, null); | ||
|
@@ -294,7 +306,11 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |
'const fs = require("node:fs"); await Promise.resolve();', | ||
]); | ||
|
||
match(stderr, /ReferenceError: require is not defined in ES module scope/); | ||
match( | ||
stderr, | ||
/SyntaxError: Top-level await is not supported in CommonJS modules\. To use top-level await, add "type": "module" to your package\.json or rename the file to use the \.mjs extension\. Alternatively, wrap the await expression in an async function\./ | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is titled “throws on undefined I think erroring about the top-level |
||
|
||
strictEqual(stdout, ''); | ||
strictEqual(code, 1); | ||
strictEqual(signal, null); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing here would hide the original error, which makes it harder for users to locate e.g. where that top level await is.