Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用webpack在nodejs应用程序中包含jquery?_Jquery_Node.js_Webpack_Webpack 2 - Fatal编程技术网

如何使用webpack在nodejs应用程序中包含jquery?

如何使用webpack在nodejs应用程序中包含jquery?,jquery,node.js,webpack,webpack-2,Jquery,Node.js,Webpack,Webpack 2,这是我的webpack.config.js var webpack = require("webpack"); module.exports = { entry: './app.js', output: { filename: './bundle.js' }, module: { loaders: [ { test: /\.js$/, excl

这是我的webpack.config.js

var webpack = require("webpack");

module.exports = {
    entry: './app.js',
    output: {
        filename: './bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            }
        ],
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ]
};
这是我的app.js

var $ = require('jquery');
global.jQuery = $;
global.$ = $;
require('jquery');
console.log('Hello from Webpack');
$('#serviceContainer').hide();
因此,当我运行node app.js启动我的节点应用程序时,它需要jquery,并且抛出以下错误

 throw new Error( "jQuery requires a window with a document" );

所以我需要知道如何使用webpack在nodejs中包含jquery。如果有人知道,请帮忙。提前谢谢

jQuery要在全局上下文中(窗口)

ProviderPlugin

new webpack.ProvidePlugin({
        '$': 'jquery',
        'jQuery': 'jquery',
    })

使用nodejs,您可以创建web应用程序的服务器端,而jquery是一个JS库,它允许您操作web页面客户端的DOM,因此jquery必须用于HTML页面。在服务器端,您必须创建一个简单的http服务器,向客户端发送正确的html页面。

为什么在nodejs应用程序中需要jquery?Nodejs在后端运行,而jquery在前端运行。您收到此错误,因为它无法检测到任何窗口,即浏览器窗口是否正确。您想用jquery做什么?也许还有另一个图书馆适合你
new webpack.ProvidePlugin({
        '$': 'jquery',
        'jQuery': 'jquery',
    })