electron-react-boilerplate: Electron 28 breaks build: ERR_UNKNOWN_FILE_EXTENSION – Unknown file extension ".ts" for …
Prerequisites
- Using npm
- Using an up-to-date
mainbranch - Using latest version of devtools. Check the docs for how to update
- Tried solutions mentioned in #400
- For issue in production release, add devtools output of
DEBUG_PROD=true npm run build && npm start
Expected Behavior
Updating electron should make the app start.
Current Behavior
I updated Electron to 28.0.0, then ran npm install. Now:
➜ npm start
> start
> ts-node ./.erb/scripts/check-port-in-use.js && npm run start:renderer
> start:renderer
> cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack serve --config ./.erb/configs/webpack.config.renderer.dev.ts
Starting preload.js builder...
Starting Main Process...
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:1212/
<i> [webpack-dev-server] On Your Network (IPv4): http://192.168.0.119:1212/
<i> [webpack-dev-server] On Your Network (IPv6): http://[fe80::1]:1212/
<i> [webpack-dev-server] Content not from webpack is served from '/Users/werner/Documents/Software/electron-react-boilerplate/public' directory
<i> [webpack-dev-server] 404s will fallback to '/index.html'
> start:main
> cross-env NODE_ENV=development electronmon -r ts-node/register/transpile-only .
> start:preload
> cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.preload.dev.ts
[electronmon] waiting for a change to restart it
App threw an error during load
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/werner/Documents/Software/electron-react-boilerplate/src/main/main.ts
at new NodeError (node:internal/errors:405:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:80:11)
at defaultGetFormat (node:internal/modules/esm/get_format:125:36)
at defaultLoad (node:internal/modules/esm/load:89:20)
at nextLoad (node:internal/modules/esm/loader:163:28)
at ESMLoader.load (node:internal/modules/esm/loader:603:26)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
at new ModuleJob (node:internal/modules/esm/module_job:64:26)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
[electronmon] uncaught exception occured
[electronmon] waiting for any change to restart the app
Steps to Reproduce
See above.
Possible Solution (Not obligatory)
Not sure what the issue is. I know that Node v18.19.0 broke ts-node with a similar error message, see https://github.com/TypeStrong/ts-node/issues/1997
Latest known good version for Electron is 27.1.3.
Context
N/A
Your Environment
- Node version : v18.19.0
- electron-react-boilerplate version or branch : main
- Operating System and version : macOS
- Link to your project : N/A
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 21
- Comments: 41 (5 by maintainers)
Thanks for your comment!
That sounds reasonable, although it was far from obvious to me how that would look like. I played around a little, and what I did was the following. Create a file
./erb/configs/webpack.config.main.dev.ts:Now change
package.jsonto point to the newly generated bundle JS file:I also had to change a few places in my application that looked like this — due to outputting the bundle in
.erb/dllnow:This works, even with hot-reloading, and I have had no issues launching the app with Electron 29 and Node v18.19.0.
The only thing that irks me is that there is no official comment from the maintainers yet. The package is lagging behind and v27 will be unsupported as of April 16, 2024. Of course, I understand it’s free, open-source software and there’s no guarantee to receive any support whatsoever. But given the various solutions proposed here, an official comment regarding what would be the recommended solution would be good.
Because
webpackneeds to usets-nodewhen usingtypescriptas the configuration language: webpack-configuration-languages. And currently it does not supportESModule, this will be a hindrance when we move fully toESModule.One more thing, it seems that node will no longer support the use of custom ESM Loader(you will get this warning):
As mentioned here, after
electron v28.0.0switches toesmodulewe will no longer be able to usets-nodeto compilets -> jsat runtime. Instead,tsshould be compiled intojsbefore running. The simplest way is that we can usewebpackortscto compiletsintojsin advance and write it to a disk file, and then executeelectron.to start the service (as far as I knowelectron-forgeuses this method).webpack.config.main.dev.tsfor the main process code (the output path must be consistent with themainattribute ofpackage.jsonstart:maincommand(If you care about the time-consuming compilation, you can use
swc-loaderto speed up the compilation process.)I’ve tried this solution myself and everything works fine. But when I fully migrated to
esmodule,webpackstarted to complain: Thewebpack.config.tsfile could not be recognized. Becausewebpackdoes not yet supportesmodulewhen usingtypescriptas the configuration language.So eventually I started to give up
ts-node&webpackand turned toviteandesbuildfor a good development experience. At the same time, I also retained the advantages ofelectron-react-boilerplate. If you are interested you can view the current complete project code: https://github.com/1111mp/nvm-desktop. Currently everything is working fine.(This comment does not in any way imply that electron-react-boilerplate is obsolete. In fact, all this has nothing to do with
electron-react-boilerplateitself, because the problem that causes incompatibility withelectron v28.0.0and above is caused by the tools that its upstream depends on. The design ofelectron-react-boilerplateis still very good.)Hey @slhck @ifree92 @1111mp I created PR with all changes and added a hot reload for main process. Even if PR isn’t merged, I hope it will bring more attention to this issue.
I ran into this issue yesterday and I was able to get it working with the following changes:
startscript, replacets-nodewithtsx.start:mainscript, add NODE_OPTION flag and minor changes to the electronmon arguments.Here is what the updated version of these scripts look like:
"start": "tsx ./.erb/scripts/check-port-in-use.js && npm run start:renderer""start:main": "cross-env NODE_ENV=development NODE_OPTIONS=\"--loader tsx\" electronmon ."I haven’t exhaustively checked for the side-effects but the development environment is working fine i.e. the files are getting transpiled and on code change, the latest changes are loaded automatically.
A few points to note:
tsxas a dev dependency.My Environment
Node version : v20.5.0 Electron: 28.2.3 electron-react-boilerplate version or branch : main Operating System and version : macOS
I hope this helps developers who stumble onto this thread in future.
@slhck, it looks like
esbuild-registerworks as an alternative tots-nodein my quick testing, here’s a branch that makes the necessary changes: https://github.com/dsanders11/electron-react-boilerplate/tree/esbuildIf you want to test it and let me know if that resolves your issues, I could open a PR on this repo with the changes.
Can confirm this PR working for me @Maks19 using Node 20 and Electron 31.3.0 Thank you very much for this 🙏 we’ve been stuck on v27 for a long time and was starting to get worried this upgrade wasnt going to be possible. We really really didnt want to have to redo everything with electron-forge etc.
For what is worth, I did that migration recently, to the
electron-forgeboilerplate with thevite-typescripttemplate, and the transition was surprisingly smooth. I hardly had to change anything serious in my code, other than to specify in the configuration that I wanted different directories formainandrenderer. I’m not an expert inwebpack, but I found theviteconfiguration much easier to understand and modify, which I was never able to do reliably withwebpack.Thanks @Maks19, that worked great for me!
Hey, I managed to reload main process too. I’m sure it’s not the best solution but at least it works for me. I run webpack in watch mode, continuously monitoring source files for changes and re-bundling the output.
And update electronmon pattern to check bundle
I’m still investigating a better way to do it. Open to any suggestions about it.
Your solution worked for me too. The only thing I want to adopt is to make a hot reload of
electronmonwork. Currently, if any changes happen insrc/main/main.ts(and all imports tree), it requires a manual restart of thenpm startcommand.I also have a native dependency (
better-sqlite3). To make it work, you need to create a symlink ofrelease/app/node_modulesinto.erb/dll/node_modulesor at least to.erb/node_modules.As you see, the boilerplate already has a script: https://github.com/electron-react-boilerplate/electron-react-boilerplate/blob/main/.erb/scripts/link-modules.ts that links
release/app/node_modulesintosrc/node_modulesto make native dependencies reachable. Since within the proposed changes the entry point moved from thesrc/main/main.tsto.erb/dll/main.bundle.dev.js, you might need to put your native dependencies somewhere in the hierarchy before reaching the root path of the project.So, the following changes worked for me:
.erb/scripts/link-modules.ts:.erb/configs/webpack.paths.ts:I tried to play around with
electron-forge, but for me, it requires too much work to move the codebase to match their configuration. I’m deeply usingelectron-builder, its publishers, configured signing & notarizing of macOS builds, assembling via NSIS the app for Windows, configured unit tests, etc. I might need weeks to migrate it 😆 Will try to stay with the existing boilerplate.Also tried to play around with
electron-vite, but the main pitfall is that it does not support typescript decorators, but I’m usingtypeormtogether withbetter-sqlite3.The bad thing is that there’s no good solution at the moment, it is time to invent your own wheels! 🚴
thanks for the push! I used your repo to help get things working… thanks. Also my dev environment feels much quickier and more snappy. For everyone else, I’ll tag my PR here so y’all can see the file changes
Following the great contribution of dsanders11 I had to change more things in the scripts section of the “package.json” file to get everything to work correctly:
That said, applying your suggested changes I still get:
No, I don’t know enough about the internals… I will post a solution should I find one, but in the meantime I’ll stay on the last 27 release.