简体   繁体   English

webpack 4 TypeError:“ Object(…)不是函数”

[英]webpack 4 TypeError: “Object(…) is not a function”

I have a simple js file that I am trying to use webpack 4 with: 我有一个简单的js文件,试图将webpack 4与以下文件一起使用:

const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin');
//const ClosurePlugin = require('closure-webpack-plugin')
const path = require('path');

module.exports = env => {
    console.log("Building with env", env)
    const config = {
        entry: {
            index: './src/index.js',
            "network-mapper": './src/network-mapper/network-mapper.js'
        },
        externals: {
            'angular': 'angular',
            'cytoscape': 'cytoscape'
        },
        output: {
            filename: '[name].[contenthash].js',
            path: path.resolve(__dirname, 'dist'),
            libraryTarget: 'umd'
        },
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: ['style-loader', 'css-loader'],
                },
                {
                    test: /\.(html)$/,
                    use: {
                        loader: 'html-loader',
                    }
                },
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    //loader: 'babel-loader'
                }
            ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                template: __dirname + '/src/public/index.html',
                filename: 'index.html',
                inject: 'body'
            }),
            new CopyPlugin([
                {from: __dirname + '/src/public'}
            ])
        ],
        optimization: {
            // We no not want to minimize our code.
            minimize: false
        }
    };
    config.mode = (env.development === 'false' ? 'production' : 'development')

    if (env.development === 'false') {
        // config.optimization = {
        //     concatenateModules: false,
        //     minimizer: [
        //         new ClosurePlugin({mode: 'AGGRESSIVE_BUNDLE'}, {
        //             // compiler flags here
        //             //
        //             // for debuging help, try these:
        //             //
        //             // formatting: 'PRETTY_PRINT'
        //             // debug: true,
        //             // renaming: false
        //         })
        //     ]
        // }
    }
    return config;
}

My Javascript is pretty simple, only 1 function. 我的Javascript很简单,只有1个函数。 It works if I remove the import and export statements to make it plain js and include it in <script> tags 如果我删除importexport语句以使其成为纯js并将其包含在<script>标记中,则它会起作用

import {cytoscape} from "cytoscape";

export function networkMap() {
// TODO
    var createNetworkType = function (id, color, description) {
        return {
            "id": id,
            "label": color.charAt(0).toUpperCase() + color.slice(1).toLowerCase(),
            "description": description,
            "className": color.toLowerCase()
        }
...

However, when I webpack it with webpack 4 and include the resulting script with <script> tags I get: 但是,当我将其与webpack 4进行webpack并包含带有<script>标签的结果脚本时,我得到:

TypeError: "Object(...) is not a function"

This is causing it - it is part of the webpack generated code: 这是造成它的原因-它是webpack生成的代码的一部分:

scope.cytoscape = Object(cytoscape__WEBPACK_IMPORTED_MODULE_0__["cytoscape"])({
                container: scope.container,
                elements: scope.elements,
                style: scope.style,
                layout: scope.layout
            });

How do I configure webpack to simply create a Javascript file that I can include with a script tag? 如何将webpack配置为仅创建可包含脚本标签的Javascript文件?

This answer does not address the OP's exact issue, but does highlight one potential cause of the error in question. 此答案未解决OP的确切问题,但突出了引起该错误的一个潜在原因。 In regards to Object(...) is not a function , I've encountered that error using CommonJS modules with Webpack when I exported the result of a function invocation as opposed to a reference to the function itself. 关于Object(...) is not a function ,当我导出函数调用的结果而不是对函数本身的引用时,在Webpack中使用CommonJS模块和Webpack时遇到了该错误。 Below is an example of code that will result in this error. 以下是将导致此错误的代码示例。

module.js module.js

function Init() {
    // function body here...
}

exports.Init = Init();

main.js main.js

import { Init } from 'module.js'
Init();

If you change the export in module.js to the code below, it fixes the issue. 如果将module.js中的导出更改为以下代码,则可以解决此问题。

exports.Init = Init; 

Exports shouldn't be a function expression. 导出不应该是函数表达式。 It should be an object. 它应该是一个对象。

module.exports = {}

Not

module.exports = () => {}

I've seen people use functions, but not function expressions. 我见过人们使用函数,但不使用函数表达式。

module.exports = function(env) {
    return {};
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 奇怪的(Webpack?)错误“ TypeError:Object(…)不是函数” - Strange (webpack?) error “TypeError: Object(…) is not a function” Uncaught TypeError: Object(...) is not a function 当与 WebPack 4 捆绑时 - Uncaught TypeError: Object(...) is not a function when bundling with WebPack 4 带有 babel、typescript 和 webpack 的简单库 - TypeError: (...) is not a function; 在 Object。<anonymous></anonymous> - Simple library with babel, typescript and webpack - TypeError: (…) is not a function; at Object.<anonymous> Uncaught TypeError: Object(…) is not a function after update webpack resolve.modules - Uncaught TypeError: Object(…) is not a function after updating webpack resolve.modules (WebPack) TypeError: Object is not a function or its return value is not iterable - (WebPack) TypeError: Object is not a function or its return value is not iterable Webpack 2.3.3-TypeError:$ export不是一个函数 - Webpack 2.3.3 - TypeError: $export is not a function 类型错误:Webpack 导入的模块不是 function - TypeError: Webpack imported module is not a function 类型错误:this._extensions.close 不是 webpack 中的函数 - TypeError: this._extensions.close is not a function in webpack 未捕获的类型错误:__webpack_require__(...).context 不是 function - Uncaught TypeError: __webpack_require__(...).context is not a function (Webpack) Uncaught TypeError: Swal.mixin is not a function - (Webpack) Uncaught TypeError: Swal.mixin is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM