简体   繁体   English

Typescript,反应,Electron:不能在模块外使用导入语句

[英]Typescript, React, Electron: Cannot use import statement outside a module

I am creating a project using node, react, electron and typescript我正在使用 node、react、electron 和 typescript 创建一个项目

I used the following tutorial to start the project: https://flaviocopes.com/react-electron/#add-electron我使用以下教程启动项目: https://flaviocopes.com/react-electron/#add-electron

Changed a few things to setup TS:更改了一些设置 TS 的内容:

  • npx create-react-app files --template typescript
  • npm install --save-dev ts-node
  • changed a script in the package.json: "electron-start": "ts-node src/start-react.ts"更改了 package.json 中的脚本: "electron-start": "ts-node src/start-react.ts"

So here is the issue所以这里是问题

  1. I want to use TS我想用 TS
  2. TS must use import/export, not require TS 必须使用导入/导出,不需要
  3. package.json must use "type": "module" for import/export to work package.json 必须使用"type": "module"才能导入/导出工作
  4. which causes this error: Unknown file extension ".ts"导致此错误: Unknown file extension ".ts"
  5. so, package.json must not use "type": "module" for TS to work所以,package.json 不能使用"type": "module"来让 TS 工作
  6. so maybe tsconfig.json should use "module": "CommonJS"所以也许 tsconfig.json 应该使用"module": "CommonJS"
  7. but react keeps changing module back to "module": "esnext"但反应不断将模块改回"module": "esnext"

So what can I do to get node to run my typescript start-react.ts file那么我该怎么做才能让节点运行我的 typescript start-react.ts文件

./package.json ./package.json

{
  "name": "files",
  "version": "0.1.0",
  "private": true,
  "homepage": "./",
  "main": "src/start.ts",
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.3",
    "@testing-library/user-event": "^12.6.0",
    "@types/jest": "^26.0.20",
    "@types/node": "^12.19.14",
    "@types/react": "^16.14.2",
    "@types/react-dom": "^16.9.10",
    "electron": "^11.2.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "typescript": "^4.1.3",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "nf start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "electron": "electron .",
    "electron-start": "ts-node src/start-react.ts",
    "react-start": "react-scripts start",
    "pack": "build --dir",
    "dist": "npm run build && build",
    "postinstall": "install-app-deps"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "electron-builder": "^22.9.1",
    "ts-node": "^9.1.1"
  },
  "build": {
    "appId": "com.electron.electron-with-create-react-app",
    "win": {
      "iconUrl": "https://cdn2.iconfinder.com/data/icons/designer-skills/128/react-256.png"
    },
    "directories": {
      "buildResources": "public"
    }
  }
}

./tsconfig.json ./tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

./src/start-react.ts ./src/start-react.ts

import * as net from "net"
import * as childProcess from "child_process"

const port:any = process.env.PORT
process.env.ELECTRON_START_URL = `http://localhost:${port}`

const client = new net.Socket()

let startedElectron = false
const tryConnection = () =>
{
    client.connect({ port }, () =>
    {
        client.end()
        if (startedElectron) return

        startedElectron = true
        childProcess.exec("npm run electron")
    })
}

tryConnection()
client.on("error", () => setTimeout(tryConnection, 1000))

./src/start.ts ./src/start.ts

import { app, BrowserWindow } from "electron"

let mainWindow: BrowserWindow | null

function createWindow()
{
    mainWindow = new BrowserWindow(
    {
        width: 800,
        height: 800,
        webPreferences: 
        {
            nodeIntegration: true
        }
    })
    
    mainWindow.loadURL(<string>process.env.ELECTRON_START_URL)
    mainWindow.on("closed", () => mainWindow = null)
}

app.on("ready", createWindow)

app.on("window-all-closed", () => process.platform !== "darwin" ? app.quit : null)

app.on("activate", () => !mainWindow ? createWindow() : null)

PS Just let me know if you need more info PS 如果您需要更多信息,请告诉我

Try adding "esModuleInterop": true, to tsconfig.json尝试添加"esModuleInterop": true,到 tsconfig.json

Use Boilerplate使用样板

I would suggest you to setup app with React Boilerplate present in the official documentation of Electron JS.我建议您使用 Electron JS 的 官方文档中的React Boilerplate设置应用程序。 The boilerplates offers TypeScript support and is standing strong at 16.7k stars.样板提供TypeScript 支持,并且在16.7k星上表现强劲。

Such boilerplates helps you save time and energy in manual setup and are highly reliable as they are curated with best practices.此类样板可帮助您在手动设置中节省时间和精力,并且高度可靠,因为它们采用了最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在模块外使用 import 语句 Electron React Typescript - Cannot use import statement outside a module Electron React Typescript 笑话:SyntaxError:无法在 React/Typescript 项目中的模块外使用 import 语句 - Jest: SyntaxError: Cannot use import statement outside a module in React/Typescript project 无法在后端使用 typescript 的模块外部使用导入语句 - Cannot use import statement outside a module using typescript on backend Typescript 与 mocha - 无法在模块外使用导入语句 - Typescript with mocha - Cannot use import statement outside a module 使用 TypeScript 和 nodemon: SyntaxError: Cannot use import statement outside a module - Using TypeScript and nodemon: SyntaxError: Cannot use import statement outside a module Typescript &amp; TypeORM:不能在模块外使用导入语句 - Typescript & TypeORM: Cannot use import statement outside a module TypeScript/Knex:不能在模块外使用 import 语句 - TypeScript/Knex: Cannot use import statement outside a module SyntaxError:无法在 NodeJS Typescript 的模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module in NodeJS Typescript 不能在模块、Typescript、WebdriverIO 之外使用导入语句 - Cannot Use Import Statement Outside Module, Typescript, WebdriverIO Mocha + TypeScript:不能在模块外使用导入语句 - Mocha + TypeScript: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM