jQuery 需要 window 和 webpack 中的文档
jQuery requires a window with a document in webpack
我正在使用 ReactJS.net(服务器端渲染),当我在 Webpack 中使用 jquery 时出现错误
这是我的错误
Error while rendering "Components.ToDoListSkeleton" to
"react_0LmYYfSk30qdrKJQe4McUQ": Error: jQuery requires a window with a
document
at module.exports (Script Document [5]:51:87) -> module.exports=global.document?factory(global,true):function(w){if(!w.document){throw
new Error("jQuery requires a window with a document");}return
factory(w);};}else {factory(global);} // Pass this if window is not
defined yet
at new ToDoListSkeleton (Script Document [5]:26:903)
at ReactCompositeComponentMixin._constructComponentWithoutOwner (Script Document [2]:8271:28)
at ReactCompositeComponentMixin._constructComponent (Script Document [2]:8253:22)
at ReactCompositeComponentMixin.mountComponent (Script Document [2]:8172:22)
at ReactReconciler.mountComponent (Script Document [2]:1977:36)
at Script Document [2]:19549:37
at Mixin.perform (Script Document [2]:3788:21)
at renderToStringImpl (Script Document [2]:19544:25)
at renderToString (Script Document [2]:19574:11)
at Script Document [7] [temp]:1:16 Line: 19549 Column:37
这是我的 webpack 配置
"use strict";
var path = require('path');
var WebpackNotifierPlugin = require('webpack-notifier');
var webpack = require("webpack");
module.exports = {
context: path.join(__dirname, 'Content'),
entry: {
server: './server'
},
output: {
path: path.join(__dirname, 'build'),
filename: '[name].bundle.js'
},
module: {
loaders: [
// Transform JSX in .jsx files
{ test: /\.jsx$/, loader: 'jsx-loader?harmony' },
{ test: require.resolve("jquery"), loader: "expose?$!expose?jQuery" }
]
},
resolve: {
// Allow require('./blah') to require blah.jsx
extensions: ['', '.js', '.jsx']
},
externals: {
//// Use external version of React (from CDN for client-side, or
//// bundled with ReactJS.NET for server-side)
react: "React"
},
plugins: [
new WebpackNotifierPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
})
]
};
如有任何帮助或建议,我们将不胜感激。
jQuery 仅设计用于浏览器,不 支持服务器端呈现。您需要从要在服务器端使用的任何代码中删除 jQuery。
我正在使用 ReactJS.net(服务器端渲染),当我在 Webpack 中使用 jquery 时出现错误 这是我的错误
Error while rendering "Components.ToDoListSkeleton" to "react_0LmYYfSk30qdrKJQe4McUQ": Error: jQuery requires a window with a document at module.exports (Script Document [5]:51:87) -> module.exports=global.document?factory(global,true):function(w){if(!w.document){throw new Error("jQuery requires a window with a document");}return factory(w);};}else {factory(global);} // Pass this if window is not defined yet at new ToDoListSkeleton (Script Document [5]:26:903) at ReactCompositeComponentMixin._constructComponentWithoutOwner (Script Document [2]:8271:28) at ReactCompositeComponentMixin._constructComponent (Script Document [2]:8253:22) at ReactCompositeComponentMixin.mountComponent (Script Document [2]:8172:22) at ReactReconciler.mountComponent (Script Document [2]:1977:36) at Script Document [2]:19549:37 at Mixin.perform (Script Document [2]:3788:21) at renderToStringImpl (Script Document [2]:19544:25) at renderToString (Script Document [2]:19574:11) at Script Document [7] [temp]:1:16 Line: 19549 Column:37
这是我的 webpack 配置
"use strict";
var path = require('path');
var WebpackNotifierPlugin = require('webpack-notifier');
var webpack = require("webpack");
module.exports = {
context: path.join(__dirname, 'Content'),
entry: {
server: './server'
},
output: {
path: path.join(__dirname, 'build'),
filename: '[name].bundle.js'
},
module: {
loaders: [
// Transform JSX in .jsx files
{ test: /\.jsx$/, loader: 'jsx-loader?harmony' },
{ test: require.resolve("jquery"), loader: "expose?$!expose?jQuery" }
]
},
resolve: {
// Allow require('./blah') to require blah.jsx
extensions: ['', '.js', '.jsx']
},
externals: {
//// Use external version of React (from CDN for client-side, or
//// bundled with ReactJS.NET for server-side)
react: "React"
},
plugins: [
new WebpackNotifierPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
})
]
};
如有任何帮助或建议,我们将不胜感激。
jQuery 仅设计用于浏览器,不 支持服务器端呈现。您需要从要在服务器端使用的任何代码中删除 jQuery。