“找不到模块:错误:无法解析电子和打字稿及webpack项目中的'electron-is-dev'“

ny6fqffe  于 2022-11-13  发布在  Webpack
关注(0)|答案(2)|浏览(167)

我使用电子和打印脚本与网络包。我会添加react.js。
但我键入了“npx webpack”,我得到了一个错误。
它说“electron-is-dev模块没有找到”,但我已经安装了它。

ERROR in ./src/main/window.ts
    Module not found: Error: Can't resolve 'electron-is-dev' in 'project-path/src/main'
     @ ./src/main/window.ts 2:0-36 12:35-40
     @ ./src/main/app.ts

我找不到解决此错误的方法。
window.ts

import { app, BrowserWindow } from 'electron';
import isDev from 'electron-is-dev';

(...)

webpack.config.js

const path = require('path');

module.exports = [
    {
        target: 'electron-main',
        entry: path.join(__dirname, 'src/main/app.ts'),
        mode: 'development',
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    include: path.join(__dirname, 'src/main'),
                    use: 'ts-loader',
                    exclude: [
                        /node_modules/,
                        path.join(__dirname, 'src/renderer')
                    ]
                }
            ]
        },
        output: {
            path: path.join(__dirname, 'build'),
            filename: 'electron.js'
        },
        resolve: {
            extensions: ['.tsx', '.ts', 'js']
        },
        node: {
            __dirname: false
        }
    }
];

还有包参:

{
    "name": "electron-react-typescript",
    "version": "0.0.1",
    "description": "",
    "main": "build/electron.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@types/electron-devtools-installer": "^2.2.0",
        "@types/react": "^16.9.19",
        "@types/react-dom": "^16.9.5",
        "@typescript-eslint/eslint-plugin": "2.x",
        "@typescript-eslint/parser": "2.x",
        "babel-eslint": "10.x",
        "concurrently": "^5.1.0",
        "electron": "^8.0.0",
        "electron-builder": "^22.3.2",
        "eslint": "6.x",
        "eslint-config-prettier": "^6.10.0",
        "eslint-config-react-app": "^5.2.0",
        "eslint-plugin-flowtype": "3.x",
        "eslint-plugin-import": "2.x",
        "eslint-plugin-jsx-a11y": "6.x",
        "eslint-plugin-react": "7.x",
        "eslint-plugin-react-hooks": "1.x",
        "html-webpack-plugin": "^3.2.0",
        "prettier": "^1.19.1",
        "source-map-loader": "^0.2.4",
        "ts-loader": "^6.2.1",
        "typescript": "^3.7.5",
        "webpack": "^4.41.5",
        "webpack-cli": "^3.3.10",
        "webpack-dev-server": "^3.10.3"
    },
    "dependencies": {
        "electron-devtools-installer": "^2.2.4",
        "electron-is-dev": "^1.1.0",
        "react": "^16.12.0",
        "react-dom": "^16.12.0"
    }
}

我用的是eslint和prettier。

8cdiaqws

8cdiaqws1#

我有一个webpack.config.js文件,我在另一个项目中使用过,所以我复制了它并编辑了一点。它起作用了!我比较了它们,发现了一个非常非常小的错误。

(...)
resolve: {
    extensions: ['.ts', 'js', '.tsx']
},
(...)

我写的是[. ts ',' js ','. tsx ']而不是[. ts','. js','tsx']。发生此错误的原因是我写的不只是一个点。

bpsygsoo

bpsygsoo2#

我也遇到了同样的问题。问题是我有electron-is-dev作为开发依赖项。
删除它并将其添加为常规依赖项似乎已经修复了它。
是的,我知道这是一个老线索,但我有一些成功的找到了解决方案,所以认为我应该分享。

相关问题