Wasm-pack: Missing wasm-bindgen file in package.json

Created on 7 Jul 2018  路  7Comments  路  Source: rustwasm/wasm-pack

Hey team! I'm hitting an issue with the packing step of wasm-pack. I think I'm missing a file in my package.json's files section. It's affecting module resolution after I npm install the output from wasm-pack pack.

Consider the test fixture js-hello-world. After wasm-pack init -t nodejs, wasm-bindgen generates a typescript file, two javascript files, and one WASM file:

$ ls -al pkg 
total 624
drwxr-xr-x  9 mciantyre  staff     288 Jul  6 17:08 .
drwxr-xr-x  9 mciantyre  staff     288 Jul  6 17:33 ..
-rw-r--r--  1 mciantyre  staff      51 Jul  6 17:57 README.md
-rw-r--r--  1 mciantyre  staff   74708 Jul  6 17:08 js-hello-world-0.1.0.tgz
-rw-r--r--  1 mciantyre  staff      65 Jul  6 17:58 js_hello_world.d.ts
-rw-r--r--  1 mciantyre  staff    4294 Jul  6 17:58 js_hello_world.js
-rw-r--r--  1 mciantyre  staff     423 Jul  6 17:58 js_hello_world_bg.js
-rw-r--r--  1 mciantyre  staff  216481 Jul  6 17:58 js_hello_world_bg.wasm
-rw-r--r--  1 mciantyre  staff     437 Jul  6 17:57 package.json

But the package.json is missing js_hello_world_bg.js:

  "files": [
    "js_hello_world_bg.wasm",
    "js_hello_world.d.ts"
  ],

It seems that if that third file isn't provided, wasm-pack pack doesn't include it in the output tar. Then, after I npm install the tar and require the package, node throws a module resolution error.

As a minimum example, from the tests/fixtures/js-hello-world example:

$ wasm-pack init -t nodejs
$ mkdir example && cd example
$ npm init -y
$ npm install ../pkg/js-hello-world-0.1.0.tgz
$ cat << EOF > index.js
  var lib = require('js-hello-world');
  EOF
$ node index.js

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module './js_hello_world_bg'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/mciantyre/Projects/wasm-pack/tests/fixtures/js-hello-world/example/node_modules/js-hello-world/js_hello_world.js:178:8)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

If I go back to pkg/package.json to make it look like

  "files": [
    "js_hello_world_bg.wasm",
    "js_hello_world.d.ts",
    "js_hello_world_bg.js" // new file here
  ],

re-pack with wasm-pack pack, and re-install the tar with npm install, everything is good.

Sorry if this is already a known issue. If it's more helpful to have another example, let me know and I can put something together.

I have what I believe to be a fix for wasm-pack. My patch worked on this example and my other use-case. Say the word, and I'll open a PR!

Versions of all the things:

  • wasm-pack 0.4.0
  • node v10.6.0
  • npm 6.1.0
  • rustc 1.28.0-nightly (e3bf634e0 2018-06-28)
  • MacOS 10.13.5
PR attached bug user report

All 7 comments

hi @mciantyre thanks for the very detailed bug report! the reason that the "js_hello_world_bg.js" is not included in files is because it is listed in the main of package.json. the file that is listed as main should never be ignored by npm... so i'm curious if that's what happening!

could you share the full generated package.json?

thanks again for the report and sorry you are running into this!

Of course! It looks like js_hello_world.js is main in package.json. But the *_bg.js qualified version is not included:

{
  "name": "js-hello-world",
  "collaborators": [
    "Ashley Williams <[email protected]>"
  ],
  "description": "an example rust->wasm crate",
  "version": "0.1.0",
  "license": "WTFPL",
  "repository": {
    "type": "git",
    "url": "https://github.com/ashleygwilliams/wasm-pack"
  },
  "files": [
    "js_hello_world_bg.wasm",
    "js_hello_world.d.ts"
  ],
  "main": "js_hello_world.js",
  "types": "js_hello_world.d.ts"
}

ah ha! so- that's the bug, if you add the _bg to your package.json this should work. i'm about to head out, but i'll mark this as a bug- and i am always happy to recieve PRs :)

the fix should just be an update to this line, https://github.com/rustwasm/wasm-pack/blob/master/src/manifest.rs#L74, i think but i haven't poked at this locally at all. i'm curious how the tests didn't catch this, so that would also be something to look at!

thanks again, and let me know if you'd like to take this on and i'll assign you

ok! so- after pulling down several of the fixes and poking at this, i realize i missed a key portion of your bug report- the --nodejs flag. when wasmpack is run with --nodejs it generates 2 js files, while the others only generate one! this is definitely a bug, and i will fix the flag's behavior and make a release. sorry it's taken a second for me to handle this- appreciate your patience!

Thanks for taking a deeper look! I'm sorry I never tested with the non-node target flag, nor considered it in some of my rework haha.

Looking forward to the 0.4.1 release!

I think this issue happens again at wasm-pack 0.9.1 now.

I think if it is not that easy to get right. Why can't we just get rid of this field altogether. "files" is an optional field anyway: https://docs.npmjs.com/cli/v6/configuring-npm/package-json#files.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mgattozzi picture mgattozzi  路  4Comments

TorstenStueber picture TorstenStueber  路  3Comments

ishitatsuyuki picture ishitatsuyuki  路  5Comments

xtuc picture xtuc  路  5Comments

marcoscaceres picture marcoscaceres  路  3Comments