Skip to content

Commit

Permalink
module: clarify require not defined error message
Browse files Browse the repository at this point in the history
Fixes: #33741
  • Loading branch information
aduh95 committed Mar 21, 2021
1 parent 5318e53 commit 6889055
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/internal/modules/esm/module_job.js
Expand Up @@ -151,7 +151,17 @@ class ModuleJob {
await this.instantiate();
const timeout = -1;
const breakOnSigint = false;
await this.module.evaluate(timeout, breakOnSigint);
try {
await this.module.evaluate(timeout, breakOnSigint);
} catch (e) {
if (e.name === 'ReferenceError' &&
e.message === 'require is not defined') {
e.message += ' in ES module scope. You can use `await import` instead' +
', or use `.cjs` extension to load the file as CommonJS ' +
'module.';
}
throw e;
}
return { module: this.module };
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/es-module/test-esm-undefined-require.js
@@ -0,0 +1,9 @@
'use strict';
const common = require('../common');
const assert = require('assert');

assert.rejects(
import('data:text/javascript,require;'),
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
/use .await import. instead/
).then(common.mustCall());

0 comments on commit 6889055

Please sign in to comment.