Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing wasm-bindgen file in package.json #199

Closed
mciantyre opened this issue Jul 6, 2018 · 7 comments · Fixed by #205
Closed

Missing wasm-bindgen file in package.json #199

mciantyre opened this issue Jul 6, 2018 · 7 comments · Fixed by #205
Assignees
Labels
bug Something isn't working PR attached there's a PR open for this issue user report
Milestone

Comments

@mciantyre
Copy link
Contributor

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
@ashleygwilliams
Copy link
Member

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!

@mciantyre
Copy link
Contributor Author

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 <ashley666ashley@gmail.com>"
  ],
  "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"
}

@ashleygwilliams
Copy link
Member

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

@ashleygwilliams ashleygwilliams added bug Something isn't working and removed waiting on response labels Jul 6, 2018
@ashleygwilliams ashleygwilliams added the PR attached there's a PR open for this issue label Jul 10, 2018
@ashleygwilliams ashleygwilliams added this to the 0.4.1 milestone Jul 10, 2018
@ashleygwilliams
Copy link
Member

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!

@mciantyre
Copy link
Contributor Author

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!

@funfoolsuzi
Copy link

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

@funfoolsuzi
Copy link

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.

kimwallmark pushed a commit to kimwallmark/rustwasm_book that referenced this issue Apr 9, 2023
wasm_game_of_life_bg.js and wasm_game_of_life_bg.wasm.d.ts are now being
generated by `wasm-pack build`.

There is some additional information about these files in
rustwasm/wasm-pack#199 and
rustwasm/wasm-pack#1193 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PR attached there's a PR open for this issue user report
Projects
None yet
3 participants