為什么require(“angular”)返回一個空對象

[英]why require(“angular”) returns an empty object


I've configured webpack like this:

我已經像這樣配置了webpack:

resolve: {
    alias: {
        angular: path.join(__dirname, './node_modules/angular/angular.js')
    }
},

and in my file I require angular like this:

在我的文件中我需要像這樣的角度:

var angular = require("angular");

But for some reason an empty object is returned, why?

但由於某種原因,返回一個空對象,為什么?

4 个解决方案

#1


4  

The other answers are not providing a working solution. It is true that Angular 1 is not working nicely with webpack out-of-the-box (see https://github.com/webpack/webpack/issues/2049), but it can be loaded with a shim. Try this webpack loader config:

其他答案沒有提供有效的解決方案。確實,Angular 1與開箱即用的webpack不兼容(參見https://github.com/webpack/webpack/issues/2049),但它可以加載一個墊片。試試這個webpack loader配置:

module: {
    loaders: [
        /*
         * Necessary to be able to use angular 1 with webpack as explained in https://github.com/webpack/webpack/issues/2049
         */
        {
            test: require.resolve('angular'),
            loader: 'exports?window.angular'
        },
    ]
},
plugins: [
    new webpack.ProvidePlugin({
        'angular': 'angular',
    }),
],

This should initialize the angular object properly instead of the default action of setting it to an empty object (which does not have a property named module).

這應該正確初始化角度對象,而不是將其設置為空對象(沒有名為module的屬性)的默認操作。

#2


4  

The other answers aren't quite accurate - it's true that the core angular.js file doesn't support CommonJS, but if you install it from NPM, a tiny wrapper file called index.js is provided. It's literally just two lines:

其他答案並不十分准確 - 核心angular.js文件確實不支持CommonJS,但如果從NPM安裝它,則會提供一個名為index.js的小包裝文件。它實際上只有兩行:

require('./angular'); // Runs angular.js, which attaches to the window object
module.exports = angular; // Exports the global variable

This allows you to use it in CommonJS environments like normal. So if you update your config like so, it should work:

這允許您在普通的CommonJS環境中使用它。因此,如果您像這樣更新配置,它應該工作:

resolve: {
    alias: {
        angular: path.join(__dirname, './node_modules/angular/index.js')
    }
},

(That said, this should be Webpack's default behaviour even if you don't alias angular, as index.js is marked as Angular's main file in its package.json - you probably can get away with just using no alias at all!)

(也就是說,這應該是Webpack的默認行為,即使你沒有別名的角色,因為index.js在它的package.json中標記為Angular的主文件 - 你可能完全不使用別名就可以逃脫!)

#3


1  

Conceptual answer -

概念性答案 -

Angular 1.x does not support CommonJS modules that's why following approach of exporting yields an empty object:

Angular 1.x不支持CommonJS模塊,這就是為什么以下導出方法產生一個空對象的原因:

var angular = require("angular");

So better omit the var angular part and just make use of require("angular");

因此,最好省略var angular part並使用require(“angular”);

#4


1  

Angular 1 doesn't support CommonJS modules, so it 'exports' an empty object.

Angular 1不支持CommonJS模塊,因此它“導出”一個空對象。

Instead, just require it (without assigning the result):

相反,只需要它(不指定結果):

 require('angular')

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/02/23/73007d9856d7ce9f390e4da426f341c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com