gpt4 book ai didi

javascript - Require 没有出现在我的代码中,但是 webpack 一直抛出错误 "require is not defined."

转载 作者:行者123 更新时间:2023-12-03 12:24:11 28 4
gpt4 key购买 nike

我正在用 React 编写一个 Electron 应用程序。我使用此命令运行开发版本:

webpack-dev-server --hot --host 0.0.0.0 --port 4000 --config=./webpack.dev.config.js

这里是 webpack.dev.config.js 文件

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { spawn } = require('child_process');
const helpers = require('./config/helpers');


// Config directories
const SRC_DIR = path.resolve(__dirname, 'src');
const OUTPUT_DIR = path.resolve(__dirname, 'dist');

// Any directories you will be adding code/files into, need to be added to this array so webpack will pick them up
const defaultInclude = [SRC_DIR];

module.exports = {
entry: SRC_DIR + '/index.js',
output: {
path: OUTPUT_DIR,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
include: defaultInclude
},
{
test: /\.jsx?$/,
use: [{ loader: 'babel-loader' }],
include: defaultInclude
},
{
test: /\.(jpe?g|png|gif)$/,
use: [{ loader: 'file-loader?name=img/[name]__[hash:base64:5].[ext]' }],
include: defaultInclude
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [{ loader: 'file-loader?name=font/[name]__[hash:base64:5].[ext]' }],
include: defaultInclude
}
]
},
target: 'electron-renderer',
plugins: [
new HtmlWebpackPlugin({
template: helpers.root('public/index.html'),
inject: 'body'
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
})
],
devtool: 'cheap-source-map',
devServer: {
contentBase: OUTPUT_DIR,
stats: {
colors: true,
chunks: false,
children: false
},
setup() {
spawn(
'electron',
['.'],
{ shell: true, env: process.env, stdio: 'inherit' }
)
.on('close', code => {
console.error("electron exited with code ", code);
process.exit(0)
})
.on('error', spawnError => console.error(spawnError));
}
}
};

Electron 浏览器打开后,在 Dev-Tools 控制台中出现以下错误。

Uncaught ReferenceError: require is not defined
at Object.url (index.js:23)
at __webpack_require__ (bootstrap:709)
at fn (bootstrap:94)
at Object../node_modules/webpack-dev-server/client/utils/createSocketUrl.js (createSocketUrl.js:4)
at __webpack_require__ (bootstrap:709)
at fn (bootstrap:94)
at Object.<anonymous> (client:20)
at Object../node_modules/webpack-dev-server/client/index.js?http://0.0.0.0:4000 (client:176)
at __webpack_require__ (bootstrap:709)
at fn (bootstrap:94)

它声称发生这种情况的地方是 index.js:23。

这是 index.js 的构建版本:

import React from "react";
import { render } from "react-dom";
import { Provider } from "react-redux";
import App from "./components/App";
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { ipcRenderer as ipc } from "electron";
import { onUpdate } from "./actions/workerActions";
import { RECEIVED_STATE } from "./actions/types";
import "./assets/css/index.css";
import rootReducer from "./reducers/rootReducer";
import defaultState from "../config/defaultstate"; //Setup redux store

const middleware = [thunk];
const store = createStore(rootReducer, defaultState, applyMiddleware(...middleware));
ipc.on(RECEIVED_STATE, arg => {
console.log("Recieved State: ", arg);
onUpdate(arg)(store.dispatch);
}); // Now we can render our application into it

render(React.createElement(Provider, {
store: store
}, React.createElement(App, null)), document.getElementById("app"));

如您所见,这里没有出现 require,除了 ipcRender 之外的所有导入都设计为在客户端运行,因此不应使用 required。我尝试注释掉 ipcRender 导入,但它导致了完全相同的错误。

最令人费解的是,即使注释掉了整个 index.js 文件,我仍然得到完全相同的错误。控制台仍然声称 block 注释包含对 require 的引用,该引用未定义。

最佳答案

如果您直接使用 webpack,请确保您的 webpack 配置中包含以下内容以您的渲染器代码为目标。

// webpack.config.js
...
module.exports = {
...
target: 'web',
...
}

如果您使用的是 Vue,那么您将需要如下内容:

// vue.config.js
...
module.exports = {
...
configureWebpack: {
target: 'web'
},
...
}

或者,在我的例子中,我使用的是 vue-cli-plugin-electron-builder因此需要以下内容:

// vue.config.js
...
module.exports = {
...
pluginOptions: {
electronBuilder: {
nodeIntegration: false,
chainWebpackRendererProcess: config => {
config.target('web');
}
}
},
...
}

关于javascript - Require 没有出现在我的代码中,但是 webpack 一直抛出错误 "require is not defined.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59537125/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com