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

Confusing error message when a non-Error value is thrown during require() #35687

Closed
evanw opened this issue Oct 16, 2020 · 0 comments
Closed
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@evanw
Copy link

evanw commented Oct 16, 2020

  • Version: 14.13.1
  • Platform: macOS
  • Subsystem:

What steps will reproduce the bug?

The problem can be reproduced with these three files:

// index.mjs 
import './cjs-file.cjs'
// cjs-file.cjs 
require('./throws')
// throws.js 
throw 'string'

Run this with node --experimental-modules index.mjs.

What is the expected behavior?

I expected to see something like this, which is what I see when I run node cjs-file.cjs:

throws.js:1
throw 'string'
^
string
(Use `node --trace-uncaught ...` to show where the exception was thrown)

What do you see instead?

I see this:

$ node --experimental-modules index.mjs 
internal/per_context/primordials.js:23
  return (thisArg, ...args) => ReflectApply(func, thisArg, args);
                               ^
 
TypeError: String.prototype.split called on null or undefined
    at split (<anonymous>)
    at internal/per_context/primordials.js:23:32
    at enrichCJSError (internal/modules/esm/translators.js:139:17)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:182:9)
    at ModuleJob.run (internal/modules/esm/module_job.js:146:23)
    at async Loader.import (internal/modules/esm/loader.js:165:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

This error message is a) super confusing and b) masks the original error.

Additional information

It looks to me like the enrichCJSError function either shouldn't be assuming that the error is an object with a stack property, or the caller should be checking for something like instanceof Error first.

@targos targos self-assigned this Oct 17, 2020
targos added a commit to targos/node that referenced this issue Oct 17, 2020
It is guaranteed that V8 throws a syntax error when `import` or `export`
is used outside of ESM.

Fixes: nodejs#35687
@PoojaDurgad PoojaDurgad added the esm Issues and PRs related to the ECMAScript Modules implementation. label Oct 19, 2020
MylesBorins pushed a commit that referenced this issue Nov 3, 2020
It is guaranteed that V8 throws a syntax error when `import` or `export`
is used outside of ESM.

Fixes: #35687

PR-URL: #35691
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
BethGriggs pushed a commit that referenced this issue Dec 8, 2020
It is guaranteed that V8 throws a syntax error when `import` or `export`
is used outside of ESM.

Fixes: #35687

PR-URL: #35691
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
BethGriggs pushed a commit that referenced this issue Dec 10, 2020
It is guaranteed that V8 throws a syntax error when `import` or `export`
is used outside of ESM.

Fixes: #35687

PR-URL: #35691
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
BethGriggs pushed a commit that referenced this issue Dec 15, 2020
It is guaranteed that V8 throws a syntax error when `import` or `export`
is used outside of ESM.

Fixes: #35687

PR-URL: #35691
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@targos targos removed their assignment Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants