Esm: error [ERR_REQUIRE_ESM] with esm/cjs hybrid module and non-hybrid cjs dependency

Created on 19 Feb 2018  路  8Comments  路  Source: standard-things/esm

graphql recently published a new version 0.13.1 with ESM .mjs support in addition to CJS https://github.com/graphql/graphql-js/pull/1244

every CJS module having a dependency on graphql is throwing an error now when loaded with std/esm.

node.js: 9.5.0
std/esm: 0.22.0 (also tried a variety of previous versions to see if it is a regression)

repro:

index.js

// hybrid module
import { execute, subscribe } from 'graphql'
// cjs module (has an internal dependency on graphql)
import apolloServerHapi from 'apollo-server-hapi'

esmrc

{
  "esm": "js",
  "cache": false
}

console

npm install @std/esm graphql apollo-server-hapi

node -r @std/esm index.js

error:

/std-esm-module-test/node_modules/graphql/index.js:1
Error [ERR_REQUIRE_ESM]: Must opt-in @std/[email protected] to load ES Module: file:///std-esm-module-test/node_modules/graphql/graphql.mjs
    at Function.module.exports.$._load (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at $.module.exports.$.require (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at $.t (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at v (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at Object.<anonymous> (/std-esm-module-test/node_modules/graphql/index.js:7:16)
    at Object.module.exports.t.a [as call] (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at j (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at Object.run (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
    at Object.<anonymous> (/std-esm-module-test/node_modules/graphql/index.js:1)
    at Object.module.exports.t.a [as call] (/std-esm-module-test/node_modules/@std/esm/esm.js:1)
bug

All 8 comments

I'll try to come up with a test case in the meanwhile without a 3rd-party-dependency.

Hi @dnalborczyk!

Could you create a simple repro repo for me to look at?
It's fine if it has a third-party dep.

@jdalton sure thing, here you go: https://github.com/dnalborczyk/stdesm-graphql

I added a couple of npm convinience scripts:

\"start-stdesm-cjs": "node -r @std/esm index.js\",
\"start-stdesm-esm": "node -r @std/esm index.mjs\",
\"start-node-esm": "node --experimental-modules index.mjs\"

_edit:_
to see it working pre-v0.13.1, just

npm install [email protected]

and change ...

import { execute, subscribe } from 'graphql'

to

import graphql from 'graphql'
const { execute, subscribe } = graphql

in index.js and index.mjs.

thanks for looking into this!!

@dnalborczyk Ok thanks!

I found the issue. It comes down to a simple check https://github.com/standard-things/esm/commit/d8518972c07bf2468e830968c0744ad3512b0457.

Update:

Hey @dnalborczyk!

I have to pay the unit test debt for this tomorrow. You wouldn't be up for creating a PR with a scenario test for this would you?

cool, thanks!!! yeah, I'll try to add a scenario test.

added scenario test https://github.com/standard-things/esm/pull/288

it's the best I could come up with. I must admit I got confused along the way ... :smiley:

v0.23.0 is released 馃帀

just confirmed, works beautifully! thank you for the fix!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kherock picture kherock  路  3Comments

ericelliott picture ericelliott  路  3Comments

OmgImAlexis picture OmgImAlexis  路  3Comments

janusqa picture janusqa  路  3Comments

greggb picture greggb  路  3Comments