Skip to content

Recognize .mjs the same as .js #7853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
surma opened this issue Jan 11, 2019 · 9 comments
Closed

Recognize .mjs the same as .js #7853

surma opened this issue Jan 11, 2019 · 9 comments
Assignees

Comments

@surma
Copy link
Collaborator

surma commented Jan 11, 2019

.mjs is the file extension that is used for ES6 modules.

Emscripten should probably output an .mjs file when -s EXPORT_ES6=1 is set.

//edit: Emscripten currently outputs LLVM IR code when specifying -o file.mjs.

@sbc100 sbc100 self-assigned this Jan 14, 2019
@sbc100
Copy link
Collaborator

sbc100 commented Jan 14, 2019

Should be an easy fix..

@kripken
Copy link
Member

kripken commented Jan 15, 2019

Sounds reasonable to support it optionally. I'm less sure we should emit it with EXPORT_ES6 - are all othe tools doing that?

@surma
Copy link
Collaborator Author

surma commented Jan 15, 2019

To load an ES6 module in Node, an .mjs extension is required. Since that decision has been made, we are making an effort to make all tooling understand this and behave similarly.

I’d prefer to automatically output .mjs when EXPORT_ES6 is set, but at the very least Emscripten should not output LLVM IR when -o something.mjs is specified 😄

cc @mathiasbynens to keep me honest.

@mathiasbynens
Copy link

@surma nailed it:

I’d prefer to automatically output .mjs when EXPORT_ES6 is set, but at the very least Emscripten should not output LLVM IR when -o something.mjs is specified 😄

@kripken
Copy link
Member

kripken commented Jan 15, 2019

Thanks, makes sense. Ok, sgtm to emit .mjs in that case, and I see @sbc100 is already assigned here.

sbc100 added a commit that referenced this issue Jan 16, 2019
Asking for a file of this extension enables EXPORT_ES6 and
MODULARIZE.

Fixes #7853
sbc100 added a commit that referenced this issue Jan 17, 2019
* Allow .mjs for js module output

Asking for a file of this extension enables EXPORT_ES6 and
MODULARIZE.

Fixes #7853
@dasa
Copy link
Contributor

dasa commented Dec 1, 2020

Should the output then work as a ES Module in Node?

I'm getting an error when I try that.

I compiled the sample shown at https://v8.dev/features/wasm-bigint using: emcc example.c --js-library example.js -o out.mjs -s WASM_BIGINT

I then started Node 14.15 using node --experimental-wasm-bigint and entered import("./out.mjs").then(m => { console.log(m.default()) }) and the output error is:

Promise { <pending> }
> (node:93802) UnhandledPromiseRejectionWarning: ReferenceError: __dirname is not defined
    at Module.<anonymous> (file:///Users/dasa/test/out.mjs:162:5)
    at REPL1:1:54

@wltsmrz
Copy link

wltsmrz commented Dec 1, 2020

Ran into that recently myself @dasa . I think the ES modules are meant to not contain Node-specific features like __dirname. The trick to get it to work apparently is:

import path from 'path'
const __dirname = path.dirname(new URL(import.meta.url).pathname)

@sbc100
Copy link
Collaborator

sbc100 commented Dec 1, 2020

Looks like something we should fix. Would you mind opening a separate issue specifically about __dirname?

@curiousdannii
Copy link
Contributor

Already raised: #11792

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants