Skip to content

Commit

Permalink
module: clarify require not defined error message
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Mar 21, 2021
1 parent baa85d9 commit 66c3a18
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 66c3a18

Please sign in to comment.