• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5const { spawn } = require('child_process');
6const assert = require('assert');
7const path = require('path');
8
9const requiring = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
10const required = path.resolve(
11  fixtures.path('/es-modules/package-type-module/cjs.js')
12);
13const pjson = path.resolve(
14  fixtures.path('/es-modules/package-type-module/package.json')
15);
16
17const basename = 'cjs.js';
18
19const child = spawn(process.execPath, [requiring]);
20let stderr = '';
21child.stderr.setEncoding('utf8');
22child.stderr.on('data', (data) => {
23  stderr += data;
24});
25child.on('close', common.mustCall((code, signal) => {
26  assert.strictEqual(code, 1);
27  assert.strictEqual(signal, null);
28
29  assert.ok(stderr.replace(/\r/g, '').includes(
30    `Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ${required}` +
31    '\nrequire() of ES modules is not supported.\nrequire() of ' +
32    `${required} from ${requiring} ` +
33    'is an ES module file as it is a .js file whose nearest parent ' +
34    'package.json contains "type": "module" which defines all .js ' +
35    'files in that package scope as ES modules.\nInstead rename ' +
36    `${basename} to end in .cjs, change the requiring code to use ` +
37    'import(), or remove "type": "module" from ' +
38    `${pjson}.\n`));
39  assert.ok(stderr.includes(
40    'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module'));
41}));
42