standard-things / esm

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

Document .mjs limitations better #851

Closed wheresrhys closed 1 week ago

wheresrhys commented 4 years ago

From the docs:

.mjs files are limited to basic functionality without support for esm options.

I'm converting a package to esm. Having converted all the src files to esm syntax, but keeping the file extensions as .js, everything works ok. However, if I rename some src files to .mjs I get errors importing other packages:

node --experimental-modules -r esm node_modules/mocha/bin/_mocha test/server.js
file:///Users/wheresrhys/Projects/fetch-mock/src/server.mjs:1
import fetch from 'node-fetch';

Error: Cannot load module from .mjs: file:///Users/wheresrhys/Projects/fetch-mock/src/lib/index.js
    at file:///Users/wheresrhys/Projects/fetch-mock/src/server.mjs:1
    at Generator.next (<anonymous>)
    at Object.<anonymous> (file:///Users/wheresrhys/Projects/fetch-mock/test/server.js:1)
    at Generator.next (<anonymous>)
    at file:///Users/wheresrhys/Projects/fetch-mock/node_modules/mocha/lib/mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (file:///Users/wheresrhys/Projects/fetch-mock/node_modules/mocha/lib/mocha.js:247:14)
    at Mocha.run (file:///Users/wheresrhys/Projects/fetch-mock/node_modules/mocha/lib/mocha.js:576:10)
    at Object.<anonymous> (file:///Users/wheresrhys/Projects/fetch-mock/node_modules/mocha/bin/_mocha:637:18)
    at Generator.next (<anonymous>)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
make: *** [test-esm] Error 1

I'm guessing the aforementioned '.mjs files are limited to basic functionality' is relevant here, but would really appreciate more details about what the behaviour should be