Skip to content

The assumption that module does not exist inside ESM context is fundamental wrong. #1145

Description

@frank-dspeed

I saw a merge with a test like

// Test import builtin modulesimport{readFileSync}from'fs';if(typeofreadFileSync!=='function')thrownewError('failed to import builtin module')if(typeofmodule!=='undefined')thrownewError('module should not exist in ESM')

And i see no problem with that test at all it is from #1136

but i need to make sure that you no where see that as general rule of thumb module and module.exports do exists inside ESM

here a example CJS loader written in ESM to load CJS inside a browser via esm

asyncfunctionrequire(path){let_module=window.module;window.module={};awaitimport(path);letexports=module.exports;window.module=_module;// restore globalreturnexports;}(async()=>{// top-level await cannot come soon enough…letx=awaitrequire("https://cdn.jsdelivr.net/gh/x/lib/index.js");})();

in nodejs we have directly

import{createRequire}from'module';

it is importent that you understand that ESM includes everything CJS includes + ESM every node builtin module exists only as CJS version and gets a additional wrapper to be ESM Compatible.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions