I'm trying to make a simple app for the first time, However I keep getting this error whenever I try to import any npm package. I'm unsure of what I did wrong because I'm using the npm package electron-reload
and that's not throwing any errors.
ERROR:
require() of ES Module
This is my tsconfig.json:
{
"pilerOptions": {
"target": "ES5",
"module": "CommonJS",
"outDir": "./app/js/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
},
"exclude": ["./app/js/**/*.js"],
"pileOnSave": true
}
This is the code in which the error is being thrown:
import Hwid from "hwid";
ipcMain.on("get-hwid", (event) => {
console.log(Hwid());
});
And lastly, this is my BroswerWindow
code:
const window = new BrowserWindow({
width: 700,
frame: false,
height: 700,
resizable: false,
transparent: true,
roundedCorners: true,
icon: path.join(__dirname, "../design/imgs/dully_logo.png"),
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
preload: path.join(__dirname, "preload.js"),
devTools: false,
},
});
window.loadFile(path.join(__dirname, "../design/index.html"));
I'm using TypeScript because I prefer it more than regular JS, I'm just stuck on what to do or why this error stops my development. I'm expecting the package to run like normal, yet nothing works.
I'm trying to make a simple app for the first time, However I keep getting this error whenever I try to import any npm package. I'm unsure of what I did wrong because I'm using the npm package electron-reload
and that's not throwing any errors.
ERROR:
require() of ES Module
This is my tsconfig.json:
{
"pilerOptions": {
"target": "ES5",
"module": "CommonJS",
"outDir": "./app/js/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
},
"exclude": ["./app/js/**/*.js"],
"pileOnSave": true
}
This is the code in which the error is being thrown:
import Hwid from "hwid";
ipcMain.on("get-hwid", (event) => {
console.log(Hwid());
});
And lastly, this is my BroswerWindow
code:
const window = new BrowserWindow({
width: 700,
frame: false,
height: 700,
resizable: false,
transparent: true,
roundedCorners: true,
icon: path.join(__dirname, "../design/imgs/dully_logo.png"),
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
preload: path.join(__dirname, "preload.js"),
devTools: false,
},
});
window.loadFile(path.join(__dirname, "../design/index.html"));
I'm using TypeScript because I prefer it more than regular JS, I'm just stuck on what to do or why this error stops my development. I'm expecting the package to run like normal, yet nothing works.
Share Improve this question asked Apr 14, 2022 at 1:42 staticstatic 271 gold badge2 silver badges7 bronze badges4 Answers
Reset to default 2Put an entry index.cjs with the code import('./index.js')
and in package.json you can already add "type": "module"
.
hwid
is an ESM module and your tsconfig.json
specifies to write modules using CommonJS, which is the require()
function. tsc
(or other pilers) converts your import
statements to require()
calls, which is not permitted with ESM (as the error says). Either use an older version of hwid
which may support ESM or change module
to 'es2020'
and set moduleResolution
to 'node'
.
Try: const Hwid = require('hwid')
You may want to check that your bundler (Webpack, Vite, etc.) works correctly, or start using one if you aren’t already.
Further reading
- This ment on Electron issue #21457