GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — esm: ensure require.main for CJS top-level loads · nodejs/node@a75e44d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules/esm
    • loader.js
    • module_job.js
    • translators.js
  • test/es-module
    • test-esm-cjs-main.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/modules/esm/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js
+1-6Lines changed: 1 addition & 6 deletions
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
}
Collapse file

‎lib/internal/modules/esm/module_job.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+5-4Lines changed: 5 additions & 4 deletions
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 {
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+2-2Lines changed: 2 additions & 2 deletions
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

Collapse file

‎test/es-module/test-esm-cjs-main.js‎

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
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');

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.