如何解决module.id should be a string error using webpack2

How to solve module.id should be a string error using webpack2

所以我在一个有多个外部依赖项的 Angular2 项目上使用 webpack2。其中一些依赖项使用 commonjs 并声明如下组件:

@Component({
    moduleId: module.id,
    templateUrl: 'mycomponent.html'
    ...
})

这会导致以下错误:

Error: moduleId should be a string in "MyComponent"

经过一些研究,我发现这是由于 Webpack 期望组件将 id 作为数字,而 Angular 将其声明为字符串。我无法更改依赖代码。我该怎么做才能忍受这种依赖?

谢谢!

删除行

moduleId: module.id,

有效。

所以这就是我现在想出的办法来忍受这种依赖。使用字符串替换加载程序为我删除该行:

{
    test: /.*node_modules\/my-dependency-folder\/.*\.js/,
    loader: 'string-replace-loader',
    query: {
        search: 'moduleId: module.id,',
        replace: ''
    }
}

希望这对遇到同样问题的人有所帮助。

我不知道这是否有帮助,但也许有人会需要它(我确实需要它!)。

所以在最坏的情况下可以在.js文件中搜索module.id并添加.toString()方法(module.id.toString()),它会解决问题。