Skip to content

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

@evanw

Description

@evanw
  • 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.

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