From 0aef97215ce50f5e40e5d31960444234ef349f8a Mon Sep 17 00:00:00 2001 From: Mark Andrus Roberts Date: Mon, 9 Apr 2018 17:38:22 -0700 Subject: [PATCH] Call fs.readFileSync with __dirname Node's fs APIs resolve relative paths relative to the current working directory: https://nodejs.org/api/fs.html#fs_file_paths This creates a problem if you try to require the wasm-bindgen-generated JavaScript from a different directory. For example, if you have build/foo.js build/foo_bg.js build/foo_bg.wasm and another script, script/index.js, that requires build/foo.js. We can instead use __dirname to get the correct path to the file. --- crates/cli-support/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 0060fd925..271bd6fd5 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -148,7 +148,8 @@ impl Bindgen { } shim.push_str(&format!(" - const bytes = require('fs').readFileSync('{}'); + const join = require('path').join; + const bytes = require('fs').readFileSync(join(__dirname, '{}')); const wasmModule = new WebAssembly.Module(bytes); const wasmInstance = new WebAssembly.Instance(wasmModule, imports); module.exports = wasmInstance.exports;