需要Webpack捆绑包-返回空对象吗? [英] Require Webpack bundle -- returns empty object?

查看:77
本文介绍了需要Webpack捆绑包-返回空对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新-相关:如何在 module.exports = 之前添加到webpack捆绑包中?

Update -- related: How to prepend module.exports = to webpack bundle?

我用webpack编译了一个简单的 module.exports = asdfasdf foo.js

I have webpack compile a simple module.exports = "asdfasdf" to foo.js

在节点 server.js 中,我有 var foo = require( ./ foo.js )

当我 console.log(foo)时,我得到一个空对象 {}

When I console.log(foo) I get an empty object {}

我在做什么错??

我的webpack配置:

My webpack config:

module.exports = {
    entry: "./test.js",
    output: {
        filename: "./foo.js"
    },
    target: "node",
    module: {
        loaders: [
            {
                exclude: /(node_modules|bower_components)/,
                loader: "babel?presets[]=react,presets[]=es2015"
            }
        ]
    },
    devtool: "#source-map"
};


推荐答案

我认为您错过了 libraryTarget -设置。在配置中添加 libraryTarget: commonjs2 应该可以解决此问题。 查看有关此问题的webpack文档

I think you are missing the libraryTarget-setting. Adding libraryTarget: "commonjs2" to the config should fix the issue. See the webpack-docs about it.

这篇关于需要Webpack捆绑包-返回空对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆