const dependencyJobs = await moduleJob.linked; return PromiseAll(new SafeArrayIterator( ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph))); }; await addJobsToDependencyGraph(this); try { if (!hasPausedEntry && this.inspectBrk) { hasPausedEntry = true; const initWrapper = internalBinding('inspector').callAndPauseOnStart; initWrapper(this.module.instantiate, this.module); } else { this.module.instantiate(); } } catch (e) { decorateErrorStack(e); // TODO(@bcoe): Add source map support to exception that occurs as result // of missing named export. This is currently not possible because // stack trace originates in module_job, not the file itself. A hidden // symbol with filename could be set in node_errors.cc to facilitate this. if (!getSourceMapsEnabled() && StringPrototypeIncludes(e.message, ' does not provide an export named')) { const splitStack = StringPrototypeSplit(e.stack, '\n'); const parentFileUrl = StringPrototypeReplace( splitStack[0], /:\d+$/, '' ); const { 1: childSpecifier, 2: name } = StringPrototypeMatch( e.message, /module '(.*)' does not provide an export named '(.+)'/); const { url: childFileURL } = await this.loader.resolve( childSpecifier, parentFileUrl, ); let format; try { // This might throw for non-CommonJS modules because we aren't passing // in the import assertions and some formats require them; but we only // care about CommonJS for the purposes of this error message. ({ format } = await this.loader.load(childFileURL)); } catch { // Continue regardless of error. } if (format === 'commonjs') { const importStatement = splitStack[1]; // TODO(@ctavan): The original error stack only provides the single // line which causes the error. For multi-line import statements we // cannot generate an equivalent object destructuring assignment by // just parsing the error stack. const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/); const destructuringAssignment = oneLineNamedImports && StringPrototypeReplace(oneLineNamedImports, /\s+as\s+/g, ': '); e.message = `Named export '${name}' not found. The requested module` + ` '${childSpecifier}' is a CommonJS module, which may not support` + ' all module.exports as named exports.\nCommonJS modules can ' + 'always be imported via the default export, for example using:' + `\n\nimport pkg from '${childSpecifier}';\n${ destructuringAssignment ? `const ${destructuringAssignment} = pkg;\n` : ''}`; const newStack = StringPrototypeSplit(e.stack, '\n'); newStack[3] = `SyntaxError: ${e.message}`; e.stack = ArrayPrototypeJoin(newStack, '\n'); } } throw e; } for (const dependencyJob of jobsInGraph) { // Calling `this.module.instantiate()` instantiates not only the // ModuleWrap in this module, but all modules in the graph. dependencyJob.instantiated = resolvedPromise; } } async run() { await this.instantiate(); const timeout = -1; const breakOnSigint = false; try { await this.module.evaluate(timeout, breakOnSigint); } catch (e) { if (e?.name === 'ReferenceError' && isCommonJSGlobalLikeNotDefinedError(e.message)) { e.message += ' in ES module scope'; if (StringPrototypeStartsWith(e.message, 'require ')) { e.message += ', you can use import instead'; } const packageConfig = StringPrototypeStartsWith(this.module.url, 'file://') && RegExpPrototypeTest(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) && require('internal/modules/esm/resolve') .getPackageScopeConfig(this.module.url); if (packageConfig.type === 'module') { e.message += '\nThis file is being treated as an ES module because it has a ' + `'.js' file extension and '${packageConfig.pjsonPath}' contains ` + '"type": "module". To treat it as a CommonJS script, rename it ' + 'to use the \'.cjs\' file extension.'; } } throw e; } return { module: this.module }; } } ObjectSetPrototypeOf(ModuleJob.prototype, null); module.exports = ModuleJob;