无法在模块外部使用import语句

无法在模块外部使用import语句

本文介绍了Typescript,React,Electron:无法在模块外部使用import语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点,反应,电子和打字稿创建项目

I am creating a project using node, react, electron and typescript

我使用以下教程启动了该项目: https://flaviocopes.com/react-electron/#add-electron

I used the following tutorial to start the project: https://flaviocopes.com/react-electron/#add-electron

更改了一些设置TS:

  • npx create-react-app文件-模板打字稿
  • npm install --save-dev ts-node
  • 更改了package.json中的脚本:"electron-start":"ts-node src/start-react.ts"

这就是问题所在

  1. 我想使用TS
  2. TS必须使用导入/导出,而不要求
  3. package.json必须使用"type":"module" 才能进行导入/导出工作
  4. 导致此错误的
  5. :文件扩展名".ts"
  6. 因此,package.json不得使用"type":"module" 来使TS正常工作
  7. 因此tsconfig.json应该使用"module":"CommonJS"
  8. 但是反应使模块不断地变回"module".:"esnext"
  1. I want to use TS
  2. TS must use import/export, not require
  3. package.json must use "type": "module" for import/export to work
  4. which causes this error: Unknown file extension ".ts"
  5. so, package.json must not use "type": "module" for TS to work
  6. so maybe tsconfig.json should use "module" : "CommonJS"
  7. but react keeps changing module back to "module" : "esnext"

那我该怎么做才能让节点运行我的打字稿 start-react.ts 文件

So what can I do to get node to run my typescript start-react.ts file

./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

{
  "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

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

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)

P.S.请让我知道是否需要更多信息

P.S. Just let me know if you need more info

推荐答案

尝试向tsconfig.json添加"esModuleInterop":true,

Try adding "esModuleInterop": true, to tsconfig.json

这篇关于Typescript,React,Electron:无法在模块外部使用import语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:01