简体   繁体   中英

“jQuery requires a window with a document” webpack 2 error

I bundled my node application with webpack2, I used JQuery with jade for my frontend. while run my application I am getting this error,

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

i used

var $ = require("jquery");

in all frontend javascript files.

I tried with this these solutions ,

this to ,

    {
        test: require.resolve('jquery'),
        loader: 'expose?$!expose?jQuery'
    },

how I can handle this problem?

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/]
    }
};

jQuery is to be used on the frontend (browser) only.

It looks like you are trying to use it in Node itself to reference back to $ .

It's important to note that jQuery does not support server side rendering.

-> var $ = require("jquery"); won't work..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM