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.

Restart (`rs`) and the Webpack plugin do not work with a native module

See original GitHub issue

Pre-flight checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project uses.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.

Electron Forge version

6.0.0-beta.67

Electron version

v20.1.1

Operating system

Windows 10 Version 21H1 Build 19043.2130

Last known working Electron Forge version

No response

Expected behavior

I have better_sqlite3 included in my project, which is based on the Webpack+Typescript template.

npm start works fine, and so does npm run package – after either of these I can access the SQLite API.

It should also work when I restart it using rs as mentioned here:

If you type rs (and hit enter) in the same terminal where you ran the start command, the running app will be terminated and restarted.

Forge plugins can override this command to run custom development logic. For example, the Webpack Plugin runs a webpack-dev-server instance to provide live reloading and HMR.

Actual behavior

I get an error message when I type rs as follows:

image

Steps to reproduce

FYI the git commit I made to include better-sqlite3 in my project is here …

… but it was easy to do, nothing special, i.e.:

  • Install better-sqlite3 as a run-time dependency
  • Install the latest Node.js with the “Windows build tools” option enabled so that it can be built for Electron
  • Include it in the Main process using import sqlite from 'better-sqlite3';

Additional information

It finds better_sqlite3.node when it starts but not when it restarts.

The error message says it’s looking for undefinedbuild/Release/better_sqlite3.node

In fact the better_sqlite3.node exists in the following directories:

C:\Users\Christopher\Source\Repos\pic>dir /s better_sqlite3.node
 Volume in drive C is Windows
 Volume Serial Number is 6EBC-4C1A

 Directory of C:\Users\Christopher\Source\Repos\pic\.webpack\main\native_modules\build\Release

05/11/2022  18:29         2,659,328 better_sqlite3.node
               1 File(s)      2,659,328 bytes

 Directory of C:\Users\Christopher\Source\Repos\pic\node_modules\better-sqlite3\build\Release

23/10/2022  09:41         2,659,328 better_sqlite3.node
               1 File(s)      2,659,328 bytes

 Directory of C:\Users\Christopher\Source\Repos\pic\out\pic-win32-x64\resources\app\.webpack\main\native_modules\build\Release

29/10/2022  09:52         2,659,328 better_sqlite3.node
               1 File(s)      2,659,328 bytes

     Total Files Listed:
               3 File(s)      7,977,984 bytes

The one which it’s successfully using when it starts is presumably the one in the .webpack\main\native_modules\ directory – and this is the path fragment that’s replaced with "undefined" during a restart.

I see there are previous issues related to rshttps://github.com/electron/forge/issues?q=rs – but they’re all close.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MarshallOfSoundcommented, Nov 9, 2022

I tested this on latest forge (inited from template, added depedency, etc.)

Had to drop to Electron 19 because that module doesn’t support Electron 20 or higher yet (https://github.com/WiseLibs/better-sqlite3/pull/870) but other than that it works fine during both start, restart and package

0reactions
cwellsxcommented, Nov 20, 2022

The problem is this line of code:

		addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));

That TypeScript statement translates to this JavaScript:

		addon = DEFAULT_ADDON || (DEFAULT_ADDON = require(__webpack_require__.ab + "build/Release/better_sqlite3.node"));

When I press rs without modifying the code, the value of __webpack_require__.ab (on my machine) is

  • 'C:\Users\Christopher\Source\Repos\pic\.webpack\main/native_modules/'

And it’s undefined when I have the problem on restart.

Maybe it’s related to this, because that’s to do with setting the __webpack_require__.ab value:

IOW maybe the @vercel/webpack-asset-relocator-loader is not being re-run or something.

I can work-around the problem using their nativeBinding option which is described here:

if you’re using a complicated build system that moves, transforms, or concatenates your JS files, better-sqlite3 might have trouble locating its native C++ addon (better_sqlite3.node). If you get an error that looks like https://github.com/JoshuaWise/better-sqlite3/issues/534#issuecomment-757907190, you can solve it by using this option to provide the file path of better_sqlite3.node (relative to the current working directory).

So for example if my code looks like this then I don’t get the error:

export function createSqlDatabase(filename: string): SqlApi {
  const options: sqlite.Options = {
    nativeBinding:
      "C:\\Users\\Christopher\\Source\\Repos\\pic\\.webpack\\main\\native_modules\\build\\Release\\better_sqlite3.node",
  };
  const db = sqlite(filename, options);

That’s not ideal, especially when the application is packaged (but a useful work-around to make rs work).

In summary I guess this problem related to the @vercel/webpack-asset-relocator-loader is not being re-run on restart.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack Plugin - Electron Forge
For the main process, type rs in the console you launched electron-forge from and Forge will restart your app for you with the...
Read more >
Electron-forge with webpack devServer - Stack Overflow
For the main process, just type rs in the console you launched electron-forge from and we will restart your app for you with...
Read more >
babel/preset-env
babel/preset-env` is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, ...
Read more >
Webpack coding assistance keeps getting disabled
Hi, I'm running IntelliJ 2018.1.5 and have a project that has its own webpack config file defined as webpack.dev.conf.js.
Read more >
razzle-start-server-webpack-plugin: Docs & Community | Openbase
razzle-start-server-webpack-plugin documentation and community, including tutorials, reviews, ... ○4.2.18 (see all)○License:MIT○TypeScript:Not Found.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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