-
Notifications
You must be signed in to change notification settings - Fork 409
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
Add type = "module" in package.json to support nodejs in bundler mode #1039
Comments
Using node 14.17.3 (the current LTS release) I was able to get node to load the "bundler" version of the package. To make it work, I had to add a I also had to work around a typescript issue: the current releases of typescript generates So with those two items added to the generated package.json, and that typescript compiler per-release, I'm able to use a single package in both nodejs and in the browser. @josephg Thanks so much for posting this: I'd never have tried this approach without your suggestion! |
It seems like you should only need that fancy version of typescript to support having mixed mode JS files. Typescript should be able to compile using esm mode in the currently released typescript versions. I haven't tried it though. |
I can't change the module type for the package doing the import (multiple unrelated import of third party packages break if I change it), so I needed an alternative. But in general, yes, compiling to an esm module should be a solution. |
I've just written up the process of getting the |
I am using this simple script after build to automatically add const fs = require("fs"); // replace with import fs from 'fs'; if you need
const packageFileContent = fs.readFileSync("./pkg/package.json", "utf-8");
const packageJSON = JSON.parse(packageFileContent);
packageJSON.type = "module";
packageJSON.main = packageJSON.module;
fs.writeFileSync("./pkg/package.json", JSON.stringify(packageJSON, null, 2), "utf-8"); |
i really don't understand why this is still an issue. this effectively breaks imports and forces all of us to do hacks and workarounds. this is a very popular package, and it definitely deserves more attention from the core team! To the core team, you don't need to implement this yourself, just MERGE THE PRs and publish it. we are happy then! |
@drager sorry to ping, but would really love to see this PR merged. We're using an npm module built with wasm-pack, and we're unable to use it natively in Node + ES Modules because it wrongly declares itself as a CommonJS module. I am more than happy to help with any polish or other tasks to get this over the finish line. |
Another temporary fix could be using the Linux "wasm-patch": "jq '.type = \"module\"' ./your-pkg/package.json > ./your-pkg/package.json.tmp && mv ./your-pkg/package.json.tmp ./your-pkg/package.json", Then run it after you run (The temporary file is necessary unless you're willing to install extra packages) |
It would be nice to be really nice to have more control over the package.json in general (including whether it's generated). For a recent project I ended up deleting it on build and just made my own wrapping package.json at the root of the project. |
This issue is a must for everyone who is learning wasm-pack for the first time. Hello, new wasmer. You can try this: npm pkg set type='module'
npm pkg set main='<your_crate>.js' |
Bumping this; closing this issue by merging any of the above PRs or implementing any of the above solutions would be appreciated by many. I'm needing WASM more and more as I get into systems + web development, and having this issue closed would resolve a major hurdle in my adoption of this. Don't mean to be a pain, and apologies if this ping comes off poorly. |
Yes, need to rebase master into the current PR in order to get that merged. |
Done. (Well, merged, not rebased, from master — but I think the PR should just be squash-merged to master anyway, right?) |
Thank you! |
💡 Feature description
When you build using the default "bundler" mode, it can almost run directly from nodejs, using nodejs's built in ESM support.
Unfortunately, the created package.json file is missing
"type": "module"
. Adding this to the created package.json not only fixes some bundler issues, but also makes nodejs understand the created package too:foo.js:
The text was updated successfully, but these errors were encountered: