Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,7 @@ const {
ArrayPrototypeUnshiftApply,
Boolean,
Error,
ErrorCaptureStackTrace,
JSONParse,
ObjectDefineProperty,
ObjectFreeze,
Expand DownExpand Up@@ -1674,6 +1675,7 @@ function getRequireESMError(mod, pkg, content, filename) {
const usesEsm = containsModuleSyntax(content, filename);
const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath,
packageJsonPath);
ErrorCaptureStackTrace(err, Module.prototype.require);
Comment thread
geeksilva97 marked this conversation as resolved.
Outdated
// Attempt to reconstruct the parent require frame.
const parentModule = Module._cache[parentPath];
if (parentModule) {
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
import nothing from 'somewhere';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
function main() {
func1(func2(func3()))
}

function func1() {
require('./app.js')
}
function func2() {}
function func3() {}

main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
import '../common/index.mjs';
import { test } from 'node:test';
import * as fixtures from '../common/fixtures.mjs';
import { spawnSync } from 'node:child_process';
import assert from 'node:assert';

test('correctly reports error for a longer stack trace', () => {
// The following regex matches the error message that is expected to be thrown
//
// package-type-module/require-esm-error-annotation/index.cjs:6
// require('./app.js')
// ^

const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/index.cjs');
const args = ['--no-experimental-require-module', fixture];
const regex = /index\.cjs:6[\n\r]+\s{2}require\('\.\/app\.js'\)[\n\r]+\s{2}\^/;

const result = spawnSync(process.execPath, args);
const stderr = result.stderr.toString();

assert.strictEqual(result.status, 1);
assert.strictEqual(result.stdout.toString(), '');
assert.match(stderr, regex);
});