אלקטרון ו-TypeScript (וכן electron-builder): "לא ניתן להשתמש בהצהרת import מחוץ למודול"


אני רוצה לבנות אפליקציית אלקטרון עם TypeScript באמצעות electron-builder. הבנייה עצמה עובדת, אבל הפעלת ה-app.exe מביאה את השגיאה הזו: "לא ניתן להשתמש בפקודת import מחוץ למודול"

אני מניח שמשהו לא בסדר עם ה-package.json או ה-tsconfig.json שלי. ניסיתי כל מה שיכולתי למצוא, אותה שגיאה. אלו ההגדרות הנוכחיות שלי:

package.json:

{
  "main": "./src/main.ts",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "lint": "tslint -c tslint.json -p tsconfig.json",
    "start": "npm run build && electron ./dist/main.js",
    "pack": "electron-builder --dir",
    "dist": "electron-builder",
    "dist:dir": "yarn dist --dir -c.compression=store -c.mac.identity=null"
  },
  "devDependencies": {
    "@types/node": "^12.12.7",
    "electron": "^8.1.0",
    "electron-builder": "^22.3.2",
    "tslint": "^5.19.0",
    "typescript": "^3.6.0"
  },
  "dependencies": {
    "@types/jquery": "^3.3.31",
    "jquery": "^3.4.1",
    "jquery-ui-dist": "^1.12.1",
    "node-rest-client": "^3.1.0"
  },
  "build": {......},
  "postinstall": "electron-builder install-app-deps"
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "suppressImplicitAnyIndexErrors": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "removeComments": true,
    "preserveConstEnums": true,
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*"]
    }
  },   
  "include": [
    "src/**/*"
  ],
  "exclude": [ "node_modules" ] 
}

תשובה #1

מצאתי פתרון: חילקתי את ספריות הפלט: /out עבור תהליך yarn start, /dist עבור תהליך yarn dist

אל תשכח לשנות את כל הנתיבים בקבצי ts לתיקיית /out