Wasm-pack: env dependency not provided

Created on 28 Nov 2019  ยท  19Comments  ยท  Source: rustwasm/wasm-pack

๐Ÿ› Bug description

Error: Cannot find module 'env'
Require stack:

  • /Users/d056831/Development/hdbconnect-wasm/pkg/hdbconnect_wasm_bg.js
  • /Users/d056831/Development/hdbconnect-wasm/pkg/hdbconnect_wasm.js
  • /Users/d056831/Development/hdbconnect-wasm/test.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:15)
    at Function.Module._load (internal/modules/cjs/loader.js:685:27)
    at Module.require (internal/modules/cjs/loader.js:838:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object. (/Users/d056831/Development/hdbconnect-wasm/pkg/hdbconnect_wasm_bg.js:6:18)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Module.require (internal/modules/cjs/loader.js:838:19)

In the file ./pkg/hdbconnect_wasm_bg.js there is arequire("env"), but it is not in package.json


const path = require('path').join(__dirname, 'hdbconnect_wasm_bg.wasm');
const bytes = require('fs').readFileSync(path);
let imports = {};
imports['./hdbconnect_wasm.js'] = require('./hdbconnect_wasm.js');
imports['env'] = require('env');

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
module.exports = wasmInstance.exports;

I tried a npm install env with no success.

๐Ÿ‘Ÿ Steps to reproduce

git clone https://github.com/PSeitz/hdbconnect-wasm

wasm-pack build --target nodejs

ts-node test.ts

๐ŸŒ Your environment

Include the relevant details of your environment.
wasm-pack version: tested with master and 0.8.1
rustc version: rustc 1.40.0-nightly (1423bec54 2019-11-05)

PR welcome bug

Most helpful comment

Any new insights to this? Running into the same issue..

All 19 comments

@ashleygwilliams Passing through and dropping a possible clue

env is added in
https://github.com/rustwasm/wasm-bindgen/blame/master/crates/threads-xform/src/lib.rs#L236

printed in
https://github.com/rustwasm/wasm-bindgen/blame/master/crates/cli-support/src/lib.rs#L726

require('env') will actually refer to this 8 years old module https://www.npmjs.com/package/env which I believe is not the intention

Should env be process.env?
If so JsGenerated.generate_node_wasm_import should need a more flexible imports definition, where other modules should be able to imports.add("process.env") instead of imports.add("env")

I am running into the same issue. Cannot run my binary on node because of require('env').

Don't know if it helps, but happens only on release. (with target web, so I should be able to run it on the browser as generated - no npm install)

@cedeber Yea, this is related to the nodejs target. When compiling to web everything works as expected.

I use the web target, and it's not working as you can see here: https://github.com/cedeber/fluss

Looks like the culpable is, or one of the optimizations in Cargo.toml (I used lto and opt-level), or wasm-opt I ran after wasm-pack. I didn't check which one exactly yet.

Hm interesting. It should not be caused by the opt-level nor the lto, cause I am also compiling for web and for the release profile I use

[profile.release]
lto = true
opt-level = "z"

I see here you are simply calling init, shouldn't you pass in a string so that the wasm is internally fetched via instantiateStreaming? At least that's what I am doing.

I have

lto = true
opt-level = "s"

so s instead of z for opt-level

I tried:

  • none: works โœ…
  • opt-level="s" alone: works โœ…
  • lto = true alone: fails โŒ

If I'm using rustc 1.44.1 I have to remove lto = true to produce a working wasm. With rustc 1.45.0-beta.3 I can leave lto = true and I get a wasm without this error.

Not sure why but I am using nightly and lto works perfectly fine for me.

This problem went away for me by updating to Rust 1.46

@jakearchibald Do you know why it's fixed? Cause it still require's env and that package does not exist, cause to me this still seems to be an issue with the glue code generated by wasm-bindgen for the node target.

No idea, sorry ๐Ÿ˜ข

Any new insights to this? Running into the same issue..

Same here! Also, do we know what exactly within Rust code triggers whether require('env') gets added to the Javascript output?

What're the odds of this being fixed in a timely fashion? My options are this being fixed this week or rewriting in JS...

I personally think this is a bug with wasm-bindgen that generates the require('env') in the binding.

Perhaps specific versions fo wasm-bindgen don't have this issue and could be used as a stop gap measure?

I have the same issue. I am using rust 1.46. It just appeared and wasn't there before.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbtcollins picture rbtcollins  ยท  3Comments

ashleygwilliams picture ashleygwilliams  ยท  3Comments

Outpox picture Outpox  ยท  4Comments

jsheard picture jsheard  ยท  4Comments

RReverser picture RReverser  ยท  3Comments