Electron 和 Webpack ENOENT:没有那个文件或目录,打开 '/path.txt'

Electron and Webpack ENOENT: no such file or directory, open '/path.txt'

我是一名尝试使用与 Webpack 捆绑在一起的 Electron(以前称为 Atom)和 Reactjs 的初学者。一切正常,直到我尝试使用 Electron 的远程模块访问我的 React 组件中的 mainWindow。

尝试导入此模块时,出现臭名昭著的错误: Uncaught Error: ENOENT: no such file or directory, open '/path.txt'

我试过重新安装 electron 并检查了 node_modules/electron,发现 path.txt 在那里。

这是我的 webpack.config.js:

var webpack = require('webpack');

module.exports = {
    context: __dirname,
    entry: {
        app: ['webpack/hot/dev-server', './src/App.jsx'],
    },
    target: 'node',
    output: {
        path: './public/built',
        filename: 'bundle.js',
        publicPath: 'http://localhost:8080/built/'
    },
    devServer: {
        contentBase: './public',
        publicPath: 'http://localhost:8080/built/'
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                include: /src/,
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'file-loader?name=[path][name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader'
            }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
}

这是我的 package.json:

{
  "name": "app",
  "version": "0.1.0",
  "main": "main.js",
  "description": "description",
  "license": "UNLICENSED",
  "repository": {
    "type": "git",
    "url": "https://github.com"
  },
  "scripts": {
    "start": "./node_modules/.bin/electron .",
    "watch": "./node_modules/.bin/webpack-dev-server"
  },
  "dependencies": {
    "electron": "^1.3.5",
    "radium": "^0.17.1",
    "react": "^15.0.1",
    "react-dom": "^15.0.1"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.7.7",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "css-loader": "^0.24.0",
    "file-loader": "^0.9.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1"
  }
}

我怀疑它可能与 package.json 中的启动脚本有关,因为 path.txt 与 node_modules/.bin/electron 不在同一目录中.但是,我还没有想出如何解决这个问题。

如果还不算晚,您只需要在 webpack.config.js

中将 "node" 目标替换为 "electron-renderer"

只需运行命令如下

cd node_modules/electron && node install.js

它将创建 path.txt 文件

对于 Mac,文件包含如下内容

dist/Electron.app/Contents/MacOS/Electron