Skip to content

EXPORT_ES6 doesn’t work in Node #14271

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 May 24, 2021 · 2 comments
Closed

EXPORT_ES6 doesn’t work in Node #14271

surma opened this issue May 24, 2021 · 2 comments

Comments

@surma
Copy link
Collaborator

surma commented May 24, 2021

Glue code with -s EXPORT_ES6 doesn’t load in Node as it relies on __dirname, which doesn’t exist for modules.

Repro

package.json to set package to module mode

{
  "name": "emscriptenes6",
  "type": "module"
}
// test.c
int thething() {
  return 4;
}

Compile with:

$ emcc -O3 --closure 1 -o test.js -s ENVIRONMENT=node -s EXPORT_ES6=1 -s EXPORTED_FUNCTIONS="_thething" test.c
// main.js
import factory from "./test.js";

async function main() {
  await factory();
}
main();

Run in Node:

node main.js                                                                                                
file:///Users/surma/src/scratch/emscriptenes6/test.js:10
var a;a||(a=typeof Module !== 'undefined' ? Module : {});var f,g;a.ready=new Promise(function(b,d){f=b;g=d});var h={},l;for(l in a)a.hasOwnProperty(l)&&(h[l]=a[l]);var m="",n,p,q,r;m=__dirname+"/";n=function(b){q||(q=require("fs"));r||(r=require("path"));b=r.normalize(b);return q.readFileSync(b,null)};p=function(b){b=n(b);b.buffer||(b=new Uint8Array(b));b.buffer||t("Assertion failed: undefined");return b};1<process.argv.length&&process.argv[1].replace(/\\/g,"/");process.argv.slice(2);
                                                                                                                                                                                     ^

ReferenceError: __dirname is not defined
    at file:///Users/surma/src/scratch/emscriptenes6/test.js:10:182
    at main (file:///Users/surma/src/scratch/emscriptenes6/main.js:4:9)
    at file:///Users/surma/src/scratch/emscriptenes6/main.js:6:1
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
@RReverser
Copy link
Collaborator

This is dupe of #11792.

@RReverser
Copy link
Collaborator

I'll close this issue for housekeeping and to keep discussion in one place.

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

2 participants