-
Notifications
You must be signed in to change notification settings - Fork 262
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
Compatibility with bundling tools like webpack and rollup #308
Comments
Is node-pre-gyp supposed to work with Webpack... or not ? I've been spending hours on getting a third party library to work, and it seems to boil down to this "package.json does not exist" issue. |
I actually experience the following error with webpack:
Coming from I think it's suggested to wrap |
I have eventually sorted it out, as far as I'm concerned. |
+1
|
No. I designed node-pre-gyp and I've never used webpack nor do I understand what it is. So, its definitely not supposed to work. That said if it is feasible to get it working, I'd review a PR with tests. Until then I'll close this issue to avoid confusion/the assumption that things should work. |
If anyone comes here looking for a solution for Webpack, this fixed the problem for us:
Simply replace |
This pull request #622 solves the main problem that prevents @mapbox/node-pre-gyp from working with webpack. webpack needs to know where package.json and the .node file are located during the build process. Right now both locations are determined at run-time instead of during the build process. Using @springmeyer a couple reasons webpack is useful include
You could argue that webpack isn't necessary (it's much more useful for front-end development than back-end development), but you might be stuck using a node framework that depends on webpack. |
To add why we use webpack, it's for deployment in lambda. |
@springmeyer any chance you can merge #622 ? |
any workaround for this ? |
Ran into this issue as well. @springmeyer @dmarcs, is there any reason why #622 was closed instead of being merged? |
Oh gosh, I ran into different issues, but I finally found a workaround by hijacking the binary through Webpack. It might be helpful to others. Feel free to optimize it and share it with the community. Some Issues I Encountered:
The Workaround:// webpack.config.mjs
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
const context = dirname(fileURLToPath(import.meta.url))
export default {
// [...]
target: 'node',
entry: {
bcrypt: {
import: resolve(context, 'node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node'),
},
// [...]
},
module: {
rules: [
{
test: /\.node$/,
type: 'asset/resource', // Possibly works with node-loader as well
generator: {
filename: pathData => `${pathData.runtime}.node`,
},
},
],
},
} // server.mts
import { promisify } from 'util'
const bcrypt = (() => {
// @ts-expect-error
const bcrypt = __non_webpack_require__('./bcrypt.node')
return {
compare: promisify(bcrypt.compare) as (data: string | Buffer, encrypted: string) => Promise<boolean>,
// [...]
}
})() After building, my
|
Not drop-in replacement, but alternative to import { createRequire } from 'module'
const bcrypt = (() => {
const bcrypt = createRequire(import.meta.url)('../dist/bcrypt.node') // import.meta.url is [...]/build/server.mjs
[...] |
A way to find binaries by contents of
package.json
files seems to be needed for compatibility with JS bundlers.As this code in gRPC's repo, we need to pass paths to
package.json
to thefind(package_json_path, opts)
function inlib/pre-binding.js
currently.However, when the paths to
package.json
are hard-coded in source files like the one of gRPC, bundling tools, such as webpack and rollup, cannot detect the dynamic loading of external files and end up creating bundled files which don't work.So, we need some function like
findByPackageInfo(package_json_content, opts)
. Then, we can change the code of gRPC like:Any thoughts?
References
The text was updated successfully, but these errors were encountered: