xtuc / webassemblyjs

Toolchain for WebAssembly

Home Page:https://webassembly.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use this project in node.js?

chehx opened this issue · comments

commented

As the title claims, I meet some trouble with running this project in node.js.

`(node:12960) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
c:\Users\parserTest.js:3
import { decode } from "./node_modules/@webassemblyjs/wasm-parser";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`

I also try to use this project in an HTML file, but it fails too.

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

I try many times and read the issues and documents to solve this problem, but I failed.

Really hope you can help me to figure it out!

Thanks!

commented

After I add "type": "module", in the package.json file, the new thing happens:

node:26528) ExperimentalWarning: The ESM module loader is experimental. import { decode } from "@webassemblyjs/wasm-parser"; ^^^^^^ SyntaxError: The requested module '@webassemblyjs/wasm-parser' does not provide an export named 'decode' at ModuleJob._instantiate (internal/modules/esm/module_job.js:96:21) at async ModuleJob.run (internal/modules/esm/module_job.js:134:20) at async Loader.import (internal/modules/esm/loader.js:179:24)

And here is the code I used:

`import { decode } from "@webassemblyjs/wasm-parser";

const decoderOpts = {};

const ast = decode("./test.wasm", decoderOpts);`

The decode is exported by @webassemblyjs/wasm-parser, but it looks like nodejs requires file to be .mjs. Could you please send a change to rename the files?

commented

The decode is exported by @webassemblyjs/wasm-parser, but it looks like nodejs requires file to be .mjs. Could you please send a change to rename the files?

Many thanks for the help. I have already figured out this problem before. But a problem raises as #799

The WAT text form can't be parsed successfully. I guess maybe the WASM updates some new features.

@chehx what is the parse failure you are seeing?

commented

@chehx what is the parse failure you are seeing?

            return new Error("\n" + (0, _helperCodeFrame.codeFrameFromSource)(source, token.loc) + "\n" + "Unexpected token in instruction argument" + ", given " + tokenToString(token));
                   ^

Error:
(module  (type $t0 (func (param i32 i32) (result i32)))  (func $add (type $t0) (param $p0 i32) (param $p1 i32) (result i32)    local.get $p0    local.get $p1    i32.add)  (export "add" (func $add)))
                                                                                                                                                ^^^^^

Unexpected token in instruction argument, given name
    at C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:946:20
    at parseFuncInstrArguments (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:947:12)
    at parseFuncInstr (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1068:37)
    at parseFunc (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1182:23)
    at walk (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1623:22)
    at parseModule (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:858:27)
    at walk (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1635:22)
    at Object.parse (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1776:15)
    at parse (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\index.js:33:20)
    at file:///C:/Users/219019055/Desktop/WASMPARSER/js-parser/test.mjs:14:13

Under my test mjs file is:


import { parse } from "@webassemblyjs/wast-parser";

var sourceCode = "(module\
  (type $t0 (func (param i32 i32) (result i32)))\
  (func $add (type $t0) (param $p0 i32) (param $p1 i32) (result i32)\
    local.get $p0\
    local.get $p1\
    i32.add)\
  (export \"add\" (func $add)))"

const ast = parse(sourceCode);