MCPcopy Create free account
hub / github.com/nodejs/node / #asyncInstantiate

Method #asyncInstantiate

lib/internal/modules/esm/module_job.js:313–388  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

311 }
312
313 async #asyncInstantiate() {
314 const jobsInGraph = new SafeSet();
315 // TODO(joyeecheung): if it's not on the async loader thread, consider this already
316 // linked.
317 const addJobsToDependencyGraph = async (moduleJob) => {
318 debug(`async addJobsToDependencyGraph() ${this.url}`, moduleJob);
319
320 if (jobsInGraph.has(moduleJob)) {
321 return;
322 }
323 jobsInGraph.add(moduleJob);
324 const dependencyJobs = isPromise(moduleJob.linked) ? await moduleJob.linked : moduleJob.linked;
325 return SafePromiseAllReturnVoid(dependencyJobs, addJobsToDependencyGraph);
326 };
327 await addJobsToDependencyGraph(this);
328
329 try {
330 if (!hasPausedEntry && this.inspectBrk) {
331 hasPausedEntry = true;
332 const initWrapper = internalBinding('inspector').callAndPauseOnStart;
333 initWrapper(this.module.instantiate, this.module);
334 } else {
335 this.module.instantiate();
336 }
337 } catch (e) {
338 decorateErrorStack(e);
339 // TODO(@bcoe): Add source map support to exception that occurs as result
340 // of missing named export. This is currently not possible because
341 // stack trace originates in module_job, not the file itself. A hidden
342 // symbol with filename could be set in node_errors.cc to facilitate this.
343 if (!getSourceMapsSupport().enabled &&
344 StringPrototypeIncludes(e.message,
345 ' does not provide an export named')) {
346 const splitStack = StringPrototypeSplit(e.stack, '\n', 2);
347 const { 1: childSpecifier, 2: name } = RegExpPrototypeExec(
348 /module '(.*)' does not provide an export named '(.+)'/,
349 e.message);
350 const moduleRequests = this.module.getModuleRequests();
351 let isCommonJS = false;
352 for (let i = 0; i < moduleRequests.length; ++i) {
353 if (moduleRequests[i].specifier === childSpecifier) {
354 isCommonJS = this.commonJsDeps[i];
355 break;
356 }
357 }
358
359 if (isCommonJS) {
360 const importStatement = splitStack[1];
361 // TODO(@ctavan): The original error stack only provides the single
362 // line which causes the error. For multi-line import statements we
363 // cannot generate an equivalent object destructuring assignment by
364 // just parsing the error stack.
365 const oneLineNamedImports = RegExpPrototypeExec(/{.*}/, importStatement);
366 const destructuringAssignment = oneLineNamedImports &&
367 RegExpPrototypeSymbolReplace(/\s+as\s+/g, oneLineNamedImports, ': ');
368 e.message = `Named export '${name}' not found. The requested module` +
369 ` '${childSpecifier}' is a CommonJS module, which may not support` +
370 ' all module.exports as named exports.\nCommonJS modules can ' +

Callers 1

#instantiateMethod · 0.95

Calls 3

decorateErrorStackFunction · 0.85
getSourceMapsSupportFunction · 0.85
instantiateMethod · 0.45

Tested by

no test coverage detected