我將我的節點應用程序與webpack2綁定在一起,我在前端使用JQuery和jade。運行一段時間我的申請,我得到這個錯誤,「jQuery需要一個包含文檔的窗口」webpack 2錯誤
node_modules\jquery\dist\jquery.js:31
throw new Error("jQuery requires a window with a document");
^
Error: jQuery requires a window with a document
我在所有前端JavaScript文件使用
var $ = require("jquery");
。
我試圖與這這些solutions,
這,
{
test: require.resolve('jquery'),
loader: 'expose?$!expose?jQuery'
},
我該怎麼處理這個問題?
webpack.config.js
var path = require('path');
var nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
target: 'node',
entry: {
app: ['babel-polyfill', './bin/run.js'],
},
output: {
path: './build',
filename: '[name].min.js'
},
externals: [nodeExternals()],
plugins: [
new HtmlWebpackPlugin()
],
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /lodash/
},
{
test: /\.jade$/,
loader: 'jade-loader'
},
{
test: /\.css$/,
use: [
{loader: 'style-loader'},
{
loader: 'css-loader',
options: {
modules: true
}
}
]
},
],
noParse: [/ws/]
}
};
參考 - http://stackoverflow.com/questions/39240241/jquery-requires-a-window-with-a-document-in-webpack –
感謝對於回覆,我通過npm安裝了jquery,並且我使用'app.use(express.static(「node_modules/jquery/dist」))將jquery傳遞給客戶端;'在從webpack獲取一些錯誤之後,我使用了這個聲明。如果我刪除了這一點,我得到這個錯誤'$(函數(){ ^ 的ReferenceError:$沒有的答覆定義 '@danjonescidtrix –