standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Issues using esm on `.mjs` files from nyc since node.js 12.2.0 #839

Open coreyfarrell opened 4 years ago

coreyfarrell commented 4 years ago

I'm unable to get nyc working with .mjs files in node.js 12.2.0+. I've created a demo repo at https://github.com/coreyfarrell/nyc-esm - npm it will show the failure:

nyc --require=esm ./ok.mjs

/usr/src/npm/failures/nyc-esm/node_modules/append-transform/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/src/npm/failures/nyc-esm/ok.mjs
    at Module._extensions..mjs (internal/modules/cjs/loader.js:804:9)
    at /usr/src/npm/failures/nyc-esm/node_modules/append-transform/index.js:62:4
    at Object.<anonymous> (/usr/src/npm/failures/nyc-esm/node_modules/append-transform/index.js:62:4)

Interestingly node --require esm ./ok.mjs in this repository does work. I'm not sure why this is handled differently. I'm working on a replacement to spawn-wrap for nyc - node-preload. This module iterates module.parent until it finds internal/preload and uses that module.require to load transpiler modules for nyc. That did cause the node-preload esm test to work but did not help with nyc. I'm hoping you have a suggestion.

I have seen odd successes in node.js 12.2.0+ when node_modules/.cache/esm is already populated from 12.1.0 or below.