How to use electron-nightly with typescript properly?

220 views Asked by At

I'm novice and can't understand how to use electron-nightly package properly. And I can't find any information about it. But I need electron-nightly due it has support of ESM.

I use typescript 5.1.3

main.ts:

`import { app } from 'electron';`

package.json:

"devDependencies": {
    "@types/node": "^20.5.9",
    "electron-nightly": "^28.0.0-nightly.20230908",
    "typescript": "~5.1.3"
}

tsconfig.json:

{
  "compilerOptions": {
    "types": [
      "node",
      "electron-nightly"
    ],
  ...
}

Compile from ts to js done without any errors, but if I try to run compiled app by electron . I get error

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\electron\cli.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

I tried:

  1. import { app } from 'electron-nightly' but got error:
`File 'c:/WORKSPACE/MyProject/node_modules/electron-nightly/electron.d.ts' is not a module.ts(2306)`
  1. npm i electron and import { app } from 'electron' but got error of definitions duplicate in electron and electron-nightly:
Definitions of the following identifiers conflict with those in another file...
  1. rename package electron-nightly into electron in my node_modules folder (please don't laugh so loud I was so desperate :))) but got error:
Error: Cannot find module 'C:\Program Files\nodejs\node_modules\electron\cli.js'

So my questions:

  • Should I install electron package with npm i electron in addition? And how should I bind my code with electron-nightly in this case?
  • Which package should be imported in main.ts? And If it's electron-nightly so how to resolve ...is not a module.ts?
  • Further I would package my app with electron-forge, so would any problems with it?

Thanks!

2

There are 2 answers

1
Ramon Soarez On

You probably didn't define in the environment variables the path of the node in your windows.

That's why the nodejs module is not found as the initial error says

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\electron\cli.js'

Try setting it like this on your windows, run this in the command line.

 SET PATH=C:\Program Files\Nodejs;%PATH%

1 - You don't need to install the electron package separately if you're using electron-nightly.

2 - The "duplicate definitions" error you are experiencing occurs because you are trying to import Electron modules from two different sources: 'electron' and 'electron-nightly'. Only use Electron Nightly

3 - Electron Forge should work fine with Electron Nightly if you have it set up correctly.

0
Bartel On

Had the same problem. E.g. I am using webpack-electron, which directly references electron, so no easy way to change that. Solved it using the alias functionality of npm and yarn:

npm:

npm install electron@npm:electron-nightly --save

yarn:

yarn add electron@npm:electron-nightly

Has the extra benefit you can just change to nightly versions without updating your code...