Skip to content

Imported modules are undefined if also used inside loader #17130

@bartosz-m

Description

@bartosz-m

I'm writing custom loader for esm modules and encountered problem.

  1. Inside loader I import module A
  2. I use loader to load module B which also uses module A.
  3. Path to module A is correctly resolved
  4. In loader module A is correctly loaded but in B its value is undefined.
  5. Removing module A from loader "fixes" problem.

I bypass problem by keeping track of what modules are used in loader and when one of them is requested I use dynamicInstantiate hook.

Minimal code for issue looks like this:

loader.mjs

import express from 'express'; // same module

export async function resolve(specifier, parentModuleURL, defaultResolver) {
  return defaultResolver(specifier, parentModuleURL);
}

issue.mjs

import express from 'express'; // same module
import assert from 'assert';

assert(express);

command

node --experimental-modules --loader ./loader.mjs ./issue.mjs

expected

(node:8534) ExperimentalWarning: The ESM module loader is experimental.

actual

(node:8534) ExperimentalWarning: The ESM module loader is experimental.
{ AssertionError [ERR_ASSERTION]: undefined == true
    at file:///home/bartek/Projekty/github/issues/issue.mjs:4:1
    at ModuleJob.run (internal/loader/ModuleJob.js:96:14)
    at <anonymous>
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: true,
  operator: '==' }
  • Version: v.9.2.0
  • Platform: Linux 4.13.0-16-generic Ubuntu 17.10 x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions