![](/img/trans.png)
[英]"require is not defined" when I import something from node_modules
[英]Angular2 require is not defined for libraries in node_modules
我正在使用angular2-seed作为项目的种子。 require
在源文件中工作得很好。 但每当我有一个新的图书馆和index.html中引用它,有弹出窗口,在控制台的错误require
没有定义。
包含Systemjs
我已经阅读了关于SO的先前答案,建议使用system.js。 systemjs已经包含在内。
Index.html
<!-- shims:js --> <script src="/node_modules/systemjs/dist/system.src.js?1458283463580"></script> <script src="/node_modules/systemjs/dist/system-polyfills.src.js?1458283463581"></script> <script src="/node_modules/reflect-metadata/Reflect.js?1458283463582"></script> <script src="/node_modules/es6-shim/es6-shim.js?1458283463582"></script> <script src="/node_modules/angular2/bundles/angular2-polyfills.js?1458283463582"></script> <!-- endinject --> <script>System.config({"defaultJSExtensions":true,"paths":{"./admin/main":"/./admin/main","angular2/*":"/angular2/*","rxjs/*":"/rxjs/*","*":"/node_modules/*"},"packages":{"angular2":{"defaultExtension":false},"rxjs":{"defaultExtension":false}},"map":{"moment":"moment/moment.js"}})</script> <!-- libs:js --> <script src="/node_modules/rxjs/bundles/Rx.js?1458283463585"></script> <script src="/node_modules/angular2/bundles/angular2.js?1458283463585"></script> <script src="/node_modules/angular2/bundles/router.js?1458283463585"></script> <script src="/node_modules/angular2/bundles/http.js?1458283463586"></script> <script src="/node_modules/ng2-bootstrap/ng2-bootstrap.js?1458283463586"></script> <script src="/node_modules/ng2-select/ng2-select.js?1458283463586"></script> <script src="/node_modules/lodash/index.js?1458283463587"></script> <script src="/node_modules/ng2-pagination/index.js?1458283463587"></script> <!-- endinject --> <!-- inject:js --> <!-- endinject --> <script> System.import('./admin/main') .catch(function (e) { console.error(e, 'Report this error at https://github.com/punchagency/staffing-client/issues'); }); </script>
错误
使用需求的来源
lodash的index.js
module.exports = require('./lodash');
同样,其他库(例如ng2-select
和ng2-bootstrap
也有类似的错误
您需要在SystemJS中配置其他依赖项,而不是将它们直接包含在script
标签中。
这是一个示例:
<script>
System.configure({
map: {
'ng2-bootstrap': 'node_modules/ng2-bootstrap',
'ng2-select': 'node_modules/ng2-select',
lodash: 'node_modules/lodash/lodash.js'
},
package: {
(...)
}
});
System.import(...);
</script>
有关更多详细信息,请参见以下问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.