“jQuery需要一个带有文档的窗口”webpack 2 error

时间:2017-04-04 14:00:01

标签: javascript jquery node.js webpack webpack-2

我将我的节点应用程序与webpack2捆绑在一起,我使用JQuery和jade作为我的前端。在运行我的应用程序时,我收到此错误,

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

我用过

  

var $ = require(“jquery”);

在所有前端javascript文件中。

我尝试了这些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/]
    }
};

1 个答案:

答案 0 :(得分:1)

jQuery仅用于前端(浏览器)。

看起来您正在尝试在Node本身中使用它来引用回 $get_company = "SELECT DISTINCT companies.company,companies.company_id FROM companies INNER JOIN target_details ON companies.company_id = target_details.company_id WHERE companies.company LIKE \"%$company%\" LIMIT 1500 OFFSET 10 "; if($run_company = $conn->query($get_company)){ while($row = $run_company->fetch_assoc()){ $data[] = $row['company'].$row['company_id']; } echo json_encode($data);

重要的是要注意jQuery不支持服务器端渲染。

- > $无效。