Skip to content

No stack trace with missing async keyword and dynamic imports #32177

Open
@sbrl

Description

@sbrl

What steps will reproduce the bug?

Consider the following code:

a.mjs

(async () => {
    "use strict";
    
	let b = await import("./b.mjs");
	await b.default();
})();

b.mjs

import fs from 'fs';

function bug() {
	// The something doesn't have to exist
	console.log(await fs.promises.readFile("/proc/cpuinfo", "utf-8"));
}
export default async function () {
	await bug();
}

Explanation

There is obviously a bug in b.mjs above. The bug() function is missing the async keyword`. However, when I attempt to execute the above, I get the following error:

(node:30870) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected reserved word
    at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
    at async link (internal/modules/esm/module_job.js:37:21)
(node:30870) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:30870) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This isn't particularly helpful. Now, consider the following snippet:

c.mjs:

import fs from 'fs';

function bug() {
	// The something doesn't have to exist
	console.log(await fs.promises.readFile("/proc/cpuinfo", "utf-8"));
}

(async () => {
    "use strict";
    
    await bug();
})();

There's bug in this one too, but executing it yields a very different and much more helpful error:

file:///tmp/c.mjs:5
	console.log(await fs.promises.readFile("/proc/cpuinfo", "utf-8"));
	            ^^^^^

SyntaxError: Unexpected reserved word
    at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
    at async link (internal/modules/esm/module_job.js:37:21)

Much better. Node gives us a helping hand by telling us where the error occurred.

How often does it reproduce? Is there a required condition?

This bug in the error message only appears to occur when a module is dynamically imported with import("./path/to/file.mjs"). Specifically, the first error message is missing this bit:

file:///tmp/c.mjs:5
	console.log(await fs.promises.readFile("/proc/cpuinfo", "utf-8"));
	            ^^^^^

...obviously the filepath and line number would be different for b.mjs is this bit was added.

What is the expected behavior?

The first error message should look like the second.

When dynamically importing a module that is missing the async keyword on a method, the error message should tell me where the error occurred.

What do you see instead?

The bit there it tells me where the "unexpected reserved word" was found is missing. See above for examples of what's gone wrong.

Additional information

If possible, when it's the await keyword that was detected as unexpected, the error message should reflect this more closely. For example, it might be helpful to say `SyntaxError: Unexpected reserved word "await" (did you forget the "async" keyword?)" or something like that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions