forge icon indicating copy to clipboard operation
forge copied to clipboard

vite-typescript template conflict with "allowImportingTsExtensions":true

Open XLin0mu opened this issue 1 year ago • 6 comments

Pre-flight checklist

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

Electron Forge version

7.2.0

Electron version

v29.0.0

Operating system

Win10 x64 Pro 22H2 19045.3208

Last known working Electron Forge version

7.2.0

Expected behavior

✔ Checking your system ✔ Locating application ✔ Loading configuration ✔ Preparing native dependencies ✔ Running generateAssets hook ......

Actual behavior

✔ Checking your system ✔ Locating application ✖ Loading configuration › Unable to use specified module loaders for ".ts". ◼ Preparing native dependencies ◼ Running generateAssets hook

An unhandled rejection has occurred inside Forge: Error: Unable to use specified module loaders for ".ts". at Object.exports.prepare (C:\Users\XLin0mu\AppData\Roaming\npm\node_modules@electron-forge\cli\node_modules\rechoir\index.js:58:11) at exports.default (C:\Users\XLin0mu\AppData\Roaming\npm\node_modules@electron-forge\cli\node_modules@electron-forge\core\src\util\forge-config.ts:117:17) at async Task.task (C:\Users\XLin0mu\AppData\Roaming\npm\node_modules@electron-forge\cli\node_modules@electron-forge\core\src\api\start.ts:63:29) at async Task.run (C:\Users\XLin0mu\AppData\Roaming\npm\node_modules@electron-forge\cli\node_modules\listr2\dist\index.cjs:978:11) at async C:\Users\XLin0mu\AppData\Roaming\npm\node_modules@electron-forge\cli\node_modules\p-map\index.js:57:22

Steps to reproduce

  1. Execute command npm init electron-app@latest DLR_SA -- --template=vite-typescript.
  2. Add this item to tsconfig.json:
{
"compilerOptions": {
//...
"allowImportingTsExtensions": true,
"noEmit": true,
//whether add "noEmit" at here won't make any different.
//...
}
  1. Run forge start script. npm run start or electron-forge start, givensame error message exclude the path.

Additional information

Full tsconfig.json file (which one make error):

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "commonjs",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "noImplicitAny": true,
    "sourceMap": true,
    "baseUrl": ".",
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,

    "noEmit": true,
    "allowImportingTsExtensions": true,
  },
  "include": ["src"]
}

XLin0mu avatar Feb 21 '24 03:02 XLin0mu

Want to share my experience with this error here;

I was running into the same error Error: Unable to use specified module loaders for ".ts". but I was not using the configuration above. What ended up being the cause was that I had removed ts-node from my package,json. When I put ts-node back the error was resolved and the app started as expected.

jpmckown avatar Mar 02 '24 23:03 jpmckown

The expectation here is that your resolver (e.g. your bundler, a runtime, or some other tool) is going to make these imports between .ts files work.

https://www.typescriptlang.org/tsconfig/#allowImportingTsExtensions

I need tsx for React. React wants a module/bundler setup while forge only works with commonjs/node? So maybe commonjs/node isn't cutting it here? If we can use module/bundler then it will "make these imports between .ts files work".

A potential workaround might be to nest the React package.json/tsconfig instead of flattening it onto electron so that both can have their own? Not really a fan of this though, nor have I gotten it to work yet.

aentwist avatar May 06 '24 23:05 aentwist

The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.

I get that when I run npm run package 😀 (although unrelated??) it shares my sentiment

aentwist avatar May 06 '24 23:05 aentwist

@jpmckown do you use ts-node in the start script, in vite config or somewhere?

rtritto avatar May 29 '24 01:05 rtritto

@jpmckown do you use ts-node in the start script, in vite config or somewhere?

It's been a while but as I recall, ts-node is a dependency for Electron itself. So, the short answer is no, I think. Just leave the scripts as-is when generated by the forge.

jpmckown avatar May 29 '24 01:05 jpmckown

Thanks, I added ts-node as dev dependency and it worked.

Edit: ts-node should be added as an optional peer dependency.

rtritto avatar May 29 '24 06:05 rtritto

This is documented here: https://www.electronforge.io/config/typescript-configuration

erickzhao avatar Jan 31 '25 22:01 erickzhao