Skip to content

Modules with EXPORT_ES6=1 do not work in NodeJS #15022

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
ibdafna opened this issue Sep 10, 2021 · 6 comments
Closed

Modules with EXPORT_ES6=1 do not work in NodeJS #15022

ibdafna opened this issue Sep 10, 2021 · 6 comments

Comments

@ibdafna
Copy link

ibdafna commented Sep 10, 2021

Hi team,

Emscripten will throw an error when attempting to instantiate modules build with EXPORT_ES6=1:

const dataModel = await createWasmDataModel()
 if (!(typeof process === "object" && typeof require === "function")) throw new Error("not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)");

My Makefile configuration:

build:
	em++ \
	--bind \
	 ${SRC_DIR}/main.cpp \
	-o ${SRC_DIR}/${MODEL_NAME}.js \
	-s MODULARIZE=1 \
	-s EXPORT_ES6=1 \
	-s USE_ES6_IMPORT_META=1 \
	-s 'EXPORT_NAME="createWasmDataModel"' \
	-s ALLOW_MEMORY_GROWTH=1 \
	-fsanitize=address \
	-std=c++17

I'm happy to take a look if someone can point me in the right direction. Many thanks!

@sbc100
Copy link
Collaborator

sbc100 commented Sep 10, 2021

Does node not define both require and process the mode you are using?

@lithdew
Copy link

lithdew commented Sep 13, 2021

Does node not define both require and process the mode you are using?

Ran into the same issue as well. In cases where NodeJS is running in ES Module mode (as opposed to CommonJS mode), require is undefined as modules may only be imported via. the import statement.

I'm not sure what command-line flag may be used to force Node to run in ES Module mode, though including "type": "module", in a package.json forces any NodeJS scripts that are run under npm/yarn to operate in ES Module mode.

To take into account NodeJS running in ES Module mode, it may be better to have the check be:

if (!(typeof process === "object" && (!require || typeof require === "function")))

EDIT: Another problem in the case NodeJS is ran with ES Modules support is that all imports need to be made at the top of the JS file too.

@kripken
Copy link
Member

kripken commented Sep 13, 2021

cc @RReverser

@sbc100
Copy link
Collaborator

sbc100 commented Sep 13, 2021

I've got a fix in flight

sbc100 added a commit that referenced this issue Sep 13, 2021
The check for `require` was being applied even in the case that it
wasn't needed.

The check for node, when running under the shell environment was out of
date with the node check that we use to define ENVIRONMENT_IS_NODE and
should not have been checking for require (which is not always present
under node these days).

Fixes: #15022
sbc100 added a commit that referenced this issue Sep 13, 2021
The check for `require` was being applied even in the case that it
wasn't needed.

The check for node, when running under the shell environment was out of
date with the node check that we use to define ENVIRONMENT_IS_NODE and
should not have been checking for require (which is not always present
under node these days).

Fixes: #15022
@RReverser
Copy link
Collaborator

RReverser commented Sep 13, 2021

I think this is a duplicate of #11792. There are a lot of checks / issues necessary for EXPORT_ES6 + Node.js to work.

sbc100 added a commit that referenced this issue Sep 13, 2021
The check for `require` was being applied even in the case that it
wasn't needed.

The check for node, when running under the shell environment was out of
date with the node check that we use to define ENVIRONMENT_IS_NODE and
should not have been checking for require (which is not always present
under node these days).

Fixes: #15022
@RReverser
Copy link
Collaborator

I'll close this issue in favour of #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

Successfully merging a pull request may close this issue.

5 participants