使用WebPack构建Angular 2时RxJS解决错误

时间:2016-12-28 04:59:54

标签: angular webpack

使用Webpack构建Angular 2应用程序时出现以下错误:

  

未找到./~/@angular/core/bundles/core.umd.js模块中的错误:错误

     

无法解析模块' rxjs / Subject'在   C:\...\node_modules\@angular\core\bundles

     

@ ./~/@angular/core/bundles/core.umd.js 7:84-107

这是我的package.json:

  ...
  "devDependencies": {
    "ts-loader": "^1.3.3",
    "typescript": "~2.0.10",
    "webpack": "^1.14.0"
  },
  "dependencies": {
    "@angular/common": "~2.4.0",
    "@angular/compiler": "~2.4.0",
    "@angular/core": "~2.4.0",
    "@angular/forms": "~2.4.0",
    "@angular/http": "~2.4.0",
    "@angular/platform-browser": "~2.4.0",
    "@angular/platform-browser-dynamic": "~2.4.0",

    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.9",
    "rxjs": "^5.0.1",
    "zone.js": "^0.7.4"
  }
}

我的webpack.config.js

module.exports = {
    entry: './src/modules/app/main.ts',
    output: {
        path: './src/dist/',
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts']
    },
    module: {
        loaders: [
            { 
                exclude: /node_modules/,
                test: /\.ts?$/, 
                loader: 'ts-loader' 
            }
        ]
    }
}

请注意,我的Angular 2应用程序位于/src/modules/app/main.ts文件夹中。

为什么Webpack无法解析RxJS依赖项?

2 个答案:

答案 0 :(得分:3)

我认为您在.js内部对于webpack配置缺少resolve扩展名,因此您Module not found获得了core.umd.js

resolve: {
    extensions: ['', '.js' , '.webpack.js', '.web.js', '.ts']
}

答案 1 :(得分:1)

您需要导入主题:

import { Subject } from 'rxjs/Subject';