Skip to content

Native support for esm clients #4788

@plastikfan

Description

@plastikfan

Attempting to import from express from an esm package, does not currently work:

import express from "express";

Taking a look at express's root index.js reveals:

module.exports = require('./lib/express');

this is not compatible with esm clients. The client needs to use the esm require workaround, but the user may not be aware of what this is. Eg, I am currently using rollup/typescript in an esm module and attempted to import express and was faced with a confusing error message that did not accurately reflect the problem at hand:

[16:21:47] Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
    at error (file:///home/plastikfan/dev/tutorials/auth0/auth0-node-express-tut/node_modules/rollup/dist/es/shared/rollup.js:10332:30)
    at Module.error (file:///home/plastikfan/dev/tutorials/auth0/auth0-node-express-tut/node_modules/rollup/dist/es/shared/rollup.js:12251:16)
    at Module.tryParse (file:///home/plastikfan/dev/tutorials/auth0/auth0-node-express-tut/node_modules/rollup/dist/es/shared/rollup.js:12654:25)
    at Module.setSource (file:///home/plastikfan/dev/tutorials/auth0/auth0-node-express-tut/node_modules/rollup/dist/es/shared/rollup.js:12557:24)
    at ModuleLoader.addModuleSource (file:///home/plastikfan/dev/tutorials/auth0/auth0-node-express-tut/node_modules/rollup/dist/es/shared/rollup.js:22021:20)

The user needs to esm require instead (but really ought not to):

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const express = require("express");

Now that esm is becoming established it would be good for express to get onboard the esm bandwagon and natively support it for a better developer exprience.

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