Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

How do we handle dynamic specifiers (without dynamic import)? #7

@targos

Description

@targos

ESM cannot load a module "synchronously" with a dynamic specifier. Until we have top-level await, I think we need a supported way to do it. Two use cases:

let env;
if (process.env.NODE_ENV === 'production') {
  env = require('./prod.js');
} else {
  env = require('./dev.js');
}
let canvas, hasCanvas;
try {
  canvas = require('canvas');
  hasCanvas = true;
} catch (e) {
  hasCanvas = false;
}

I can think of three solutions:

  1. Explain in the documentation that one has to write the dynamic code in a commonJS module and import it.
  2. Add import.meta.require
  3. Add a builtin module that provides the require function (npm proposal)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions