Emscripten: Including an emscripten module in browserify build process

Created on 28 Nov 2017  路  2Comments  路  Source: emscripten-core/emscripten

Hi all,

First of all, I would like to apologize if this is not the right place to ask for help to solve this issue. I'm a browserify user, and since I have problems just with a file create by emscripten, I assume the problem might live here.

Long story short, I'm importing an emscripten module in the frontend using browserify. At execution time, the module exists, but empty.

You can reproduce the issue as follows (requires emcc and browserify installed system wide):

  • Get the code from this repo

    • Compile this file using emcc. I've used this command: emcc main.cpp -o main.js -s EXPORTED_FUNCTIONS="['_int_sqrt']"

    • Create a bundle using browserify (which must be installed system wide): browserify index.js > bundle.js

    • Open index.html in a browser, and you'll see an error in the developer console and one array showing the content of the Module object (this is, an empty array)

I've already tested that the generated code is correct: running in a node environment, works as expected (there is a demonstration using mocha here

PD: I have the same test case, but with a more elaborated toolchain in this repo with a readme to reproduce all the steps

Most helpful comment

module.exports is set only when ENVIRONMENT_IS_NODE is true, and it won't be true if ENVIRONMENT_IS_WEB is true:

  ENVIRONMENT_IS_WEB = typeof window === 'object';
  ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
  ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
  ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;

I think it would be best to always set module.exports (if module exists etc), like it does for MODULARIZE.

Turning MODULARIZE on will probably help you @txusinho, but you will need to access the exported functions differently.

All 2 comments

module.exports is set only when ENVIRONMENT_IS_NODE is true, and it won't be true if ENVIRONMENT_IS_WEB is true:

  ENVIRONMENT_IS_WEB = typeof window === 'object';
  ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
  ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
  ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;

I think it would be best to always set module.exports (if module exists etc), like it does for MODULARIZE.

Turning MODULARIZE on will probably help you @txusinho, but you will need to access the exported functions differently.

@curiousdannii I appreciate your help, and the speed! The solution worked perfect on my side, and I'm able to use module as needed. The access to functions is not a problem if I have possibilities to access the Module element.

I made this question also in stackoverflow, I'll update the answer there so anyone with the same problem can come to a solution.

EDIT: Sorry for coming back, but after inspecting the code generated, I can't find in the instance of Module any reference to the function described. Is there something I might be missing?

EDIT 2: I replaced the flag instead of adding it. Forget what I said

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ShawZG picture ShawZG  路  4Comments

nerddan picture nerddan  路  4Comments

napalm272 picture napalm272  路  4Comments

kripken picture kripken  路  4Comments

void4 picture void4  路  3Comments