最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WASM and Node.js Cannot use 'import.meta' outside a module - Stack Overflow

programmeradmin3浏览0评论

I have built FastText C++ module as wasm module using the provided make file, that is using the following flags:

EMCXX = em++
EMCXXFLAGS = --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -Isrc/
EMOBJS = args.bc autotune.bc matrix.bc dictionary.bc loss.bc productquantizer.bc densematrix.bc quantmatrix.bc vector.bc model.bc utils.bc meter.bc fasttext.bc main.bc

The piled wasm module is available here. When I run the module in the provided example predict.js I get a

  var _scriptDir = import.meta.url;
                          ^^^^

SyntaxError: Cannot use 'import.meta' outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._pile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)

caused by

var FastTextModule = (function() {
  var _scriptDir = import.meta.url;
  
  return (
function(FastTextModule) {
  FastTextModule = FastTextModule || {};

...

NOTE.

I had to adapt the piled module from the original to Node.JS in order to support require instead of import, but this should not be related to that error.

UPDATE.

I have tried the flag USE_ES6_IMPORT_META adding to the makefile -s "USE_ES6_IMPORT_META=0" ad described:

em++ --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -s "USE_ES6_IMPORT_META=0" -Isrc/  src/args -o args.bc
em++: warning: assuming object file output, based on output filename alone.  Add an explict `-c`, `-r` or `-shared` to avoid this warning [-Wemcc]

This time it seems to work, because I get a different error, that is related to the module / code:

TypeError: fastTextModule.addOnPostRun is not a function

offending line is here:

fastTextModule.addOnPostRun(() => {
  if (postRunFunc) {
    postRunFunc();
  }
});

while in the modularized file that function is defined here

Module["addOnPostRun"] = addOnPostRun;

A solution to this problem has been provided here. The final working model is here.

I have built FastText C++ module as wasm module using the provided make file, that is using the following flags:

EMCXX = em++
EMCXXFLAGS = --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -Isrc/
EMOBJS = args.bc autotune.bc matrix.bc dictionary.bc loss.bc productquantizer.bc densematrix.bc quantmatrix.bc vector.bc model.bc utils.bc meter.bc fasttext.bc main.bc

The piled wasm module is available here. When I run the module in the provided example predict.js I get a

  var _scriptDir = import.meta.url;
                          ^^^^

SyntaxError: Cannot use 'import.meta' outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._pile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)

caused by

var FastTextModule = (function() {
  var _scriptDir = import.meta.url;
  
  return (
function(FastTextModule) {
  FastTextModule = FastTextModule || {};

...

NOTE.

I had to adapt the piled module from the original to Node.JS in order to support require instead of import, but this should not be related to that error.

UPDATE.

I have tried the flag USE_ES6_IMPORT_META adding to the makefile -s "USE_ES6_IMPORT_META=0" ad described:

em++ --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPostRun', 'FS']" -s "DISABLE_EXCEPTION_CATCHING=0" -s "EXCEPTION_DEBUG=1" -s "FORCE_FILESYSTEM=1" -s "MODULARIZE=1" -s "EXPORT_ES6=1" -s 'EXPORT_NAME="FastTextModule"' -s "USE_ES6_IMPORT_META=0" -Isrc/  src/args -o args.bc
em++: warning: assuming object file output, based on output filename alone.  Add an explict `-c`, `-r` or `-shared` to avoid this warning [-Wemcc]

This time it seems to work, because I get a different error, that is related to the module / code:

TypeError: fastTextModule.addOnPostRun is not a function

offending line is here:

fastTextModule.addOnPostRun(() => {
  if (postRunFunc) {
    postRunFunc();
  }
});

while in the modularized file that function is defined here

Module["addOnPostRun"] = addOnPostRun;

A solution to this problem has been provided here. The final working model is here.

Share Improve this question edited Jan 14, 2021 at 11:17 loretoparisi asked Jan 11, 2021 at 12:17 loretoparisiloretoparisi 16.3k12 gold badges112 silver badges158 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Emscripten provide a USE_ES6_IMPORT_META flag! Maybe this can solve your problem. Take a look at https://github./emscripten-core/emscripten/blob/master/src/settings.js. There is a simple explanation about this flag.

UPDATE

Use USE_ES6_IMPORT_META=0

发布评论

评论列表(0)

  1. 暂无评论