Skip to content

Cannot use mjs file synchronously in commonJS without await / .then() #33605

Description

@Raynos
  • Version: v12.17.0
  • Platform: Linux
  • Subsystem: Ubuntu

What steps will reproduce the bug?

Require an mjs file.

constisPromise=require('./third-party-is-promise.mjs')console.log(typeofisPromise)

Example https://gist.github.com/Raynos/2c6bb0a8537dbf7d464a9b85813cd3b1

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

I can load an ESM module from a commonJS file.

What do you see instead?

~/tmp/temp123
$ node broken.cjs internal/modules/cjs/loader.js:984
throw new ERR_REQUIRE_ESM(filename);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/raynos/tmp/temp123/third-party-is-promise.mjs
at Module.load (internal/modules/cjs/loader.js:984:11)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/raynos/tmp/temp123/broken.cjs:1:19)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'ERR_REQUIRE_ESM'
}

Additional information

I would like to import an ESM synchronously from a commonJS file.

Node added child_process.execSync() a while ago but that used to not be possible.

It would be really nice if node had some kind of importSync() feature.

I also tried

constisPromise=import('./third-party-is-promise.mjs')console.log(typeofisPromise)console.log('then method',typeofisPromise.then)

This returns a promise instead of the code which is not what I want

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