question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility with bundling tools like webpack and rollup

See original GitHub issue

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 the find(package_json_path, opts) function in lib/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:

var binary = require('node-pre-gyp/lib/pre-binding');
var path = require('path');
var binding_path = binary.findByPackageInfo(require('../../../package.json'));
var binding = require(binding_path);

Any thoughts?

References

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
rajivshah3commented, Mar 26, 2021

If anyone comes here looking for a solution for Webpack, this fixed the problem for us:

I solved it by adding

externals: { 'sqlite3':'commonjs sqlite3', }

to the webpack config file

Simply replace sqlite3 with the the dependency that uses node-pre-gyp (in our case, argon2)

4reactions
dmarcscommented, Nov 27, 2021

This pull request https://github.com/mapbox/node-pre-gyp/pull/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 externals as described above is a valid workaround, but it requires that the package is available at run-time. This means that after you build with webpack, you need to run npm install package. It would be better if we could avoid npm install package since it adds an extra step.

@springmeyer a couple reasons webpack is useful include

  1. Say you have a node app written in Typescript. You normally have to convert the Typescript (server.ts) to Javascript (server.js), and then run node server.js. If you make a change to server.ts you now need to recompile the Typescript to Javascript, stop server.js, and re-run server.js. With webpack you can make a change to server.ts, and you don’t need to recompile, stop the server, and re-run the server. It does all of those steps for you. You could use nodemon and typescript watch instead of webpack for this though.

  2. If you have a node project with many relative imports, for example import '../../../../src/file.js' the code can become unreadable. With webpack we can get rid of every …/ and write code that looks cleaner, import 'src/file.js' in this example.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparing bundlers: Webpack, Rollup & Parcel | js@imaginea
Module Bundling, on a high level, is a process of integrating together a group of modules in a single file so that multiple...
Read more >
Rollup vs. Webpack -- A Comparison - OpenReplay Blog
Let's examine how these tools for managing dependencies operate. Dependency graph generation and eventual bundling are the two stages of a ...
Read more >
a guide to build tools + exploring Webpack, Parcel, Rollup, ES ...
This is post # 54 of the series, dedicated to exploring JavaScript and its building components. In the process of identifying and describing ......
Read more >
Rollup vs. Parcel vs. webpack: Which Is the Best Bundler?
For those coming from a non-JavaScript background, a bundler is a tool that ... Suppose your code is not bundled and hosted as...
Read more >
Comparing the New Generation of Build Tools - CSS-Tricks
Whether we use webpack, Rollup, or Parcel for a development server, ... esbuild works with JSON files and can bundle them into JavaScript ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found