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

时间:2016-09-09 21:16:00

标签: node.js reactjs webpack electron

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

试图导入这个模块,我得到了臭名昭着的错误: Uncaught Error: ENOENT: no such file or directory, open '/path.txt'

我尝试重新安装电子,并检查了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不在同一目录中。但是,我还没弄清楚如何解决这个问题。

2 个答案:

答案 0 :(得分:8)

如果不迟到,您只需要在webpack.config.js中用“electron-renderer”替换“node”目标

答案 1 :(得分:0)

只需运行如下命令

cd node_modules/electron && node install.js

它将创建path.txt文件

对于Mac,文件包含如下

dist/Electron.app/Contents/MacOS/Electron