◐ Shell
clean mode source ↗

esm: ensure require.main for CJS top-level loads · nodejs/node@a75e44d

Original file line numberDiff line numberDiff line change

@@ -110,12 +110,7 @@ class Loader {

110110

loaderInstance = translators.get(format);

111111

}

112112
113-

let inspectBrk = false;

114-

if (process._breakFirstLine) {

115-

delete process._breakFirstLine;

116-

inspectBrk = true;

117-

}

118-

job = new ModuleJob(this, url, loaderInstance, inspectBrk);

113+

job = new ModuleJob(this, url, loaderInstance, parentURL === undefined);

119114

this.moduleMap.set(url, job);

120115

return job;

121116

}

Original file line numberDiff line numberDiff line change

@@ -12,15 +12,15 @@ const resolvedPromise = SafePromise.resolve();

1212

class ModuleJob {

1313

// `loader` is the Loader instance used for loading dependencies.

1414

// `moduleProvider` is a function

15-

constructor(loader, url, moduleProvider, inspectBrk) {

15+

constructor(loader, url, moduleProvider, isMain) {

1616

this.loader = loader;

1717

this.error = null;

1818

this.hadError = false;

19-

this.inspectBrk = inspectBrk;

19+

this.isMain = isMain;

2020
2121

// This is a Promise<{ module, reflect }>, whose fields will be copied

2222

// onto `this` by `link()` below once it has been resolved.

23-

this.modulePromise = moduleProvider(url);

23+

this.modulePromise = moduleProvider(url, isMain);

2424

this.module = undefined;

2525

this.reflect = undefined;

2626

@@ -82,7 +82,8 @@ class ModuleJob {

8282

throw e;

8383

}

8484

try {

85-

if (this.inspectBrk) {

85+

if (this.isMain && process._breakFirstLine) {

86+

delete process._breakFirstLine;

8687

const initWrapper = process.binding('inspector').callAndPauseOnStart;

8788

initWrapper(this.module.instantiate, this.module);

8889

} else {

Original file line numberDiff line numberDiff line change

@@ -36,7 +36,7 @@ translators.set('esm', async (url) => {

3636

// Strategy for loading a node-style CommonJS module

3737

const isWindows = process.platform === 'win32';

3838

const winSepRegEx = /\//g;

39-

translators.set('cjs', async (url) => {

39+

translators.set('cjs', async (url, isMain) => {

4040

debug(`Translating CJSModule ${url}`);

4141

const pathname = internalURLModule.getPathFromURL(new URL(url));

4242

const module = CJSModule._cache[

@@ -51,7 +51,7 @@ translators.set('cjs', async (url) => {

5151

// we don't care about the return val of _load here because Module#load

5252

// will handle it for us by checking the loader registry and filling the

5353

// exports like above

54-

CJSModule._load(pathname);

54+

CJSModule._load(pathname, undefined, isMain);

5555

});

5656

});

5757
Original file line numberDiff line numberDiff line change

@@ -0,0 +1,6 @@

1+

// Flags: --experimental-modules

2+

'use strict';

3+

require('../common');

4+

const assert = require('assert');

5+

exports.asdf = 'asdf';

6+

assert.strictEqual(require.main.exports.asdf, 'asdf');