我有一个使用TypeScript和Angular 7的项目,试图在Visual Studio中进行所有设置。该项目最初是为所有旧版本创建的,我正在尝试对其进行更新并使其运行。
我在Chrome中遇到以下错误:
ReferenceError: require is not defined
at main.ts:1
还有Edge中的类似产品:
[object Error]: {
description: "'require' is not defined",
message: "'require' is not defined",
number: -2146823279,
stack: "ReferenceError: 'require' is not defined at Global code (blah … /main.js:3:1) }
从网上阅读的内容来看,我认为应该用SystemJS进行整理,以便通常服务器端的内容在浏览器中都能正常工作。我非常确定正在加载systemjs并正在执行 ,因为如果我调整配置,行为会有所不同,并且看到期望加载的相关JS文件正在加载(在浏览器中查看)开发人员工具的“网络”标签)
{
"paths": {
"npm": "/node_modules/"
},
"map": {
"app": "/Scripts/Discussion",
"@angular/core": "npm:@angular/core/bundles/core.umd.js",
"@angular/common": "npm:@angular/common/bundles/common.umd.js",
"@angular/compiler": "npm:@angular/compiler/bundles/compiler.umd.js",
"@angular/platform-browser": "npm:@angular/platform-browser/bundles/platform-browser.umd.js",
"@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js",
"@angular/http": "npm:@angular/http/bundles/http.umd.js",
"@angular/router": "npm:@angular/router/bundles/router.umd.js",
"@angular/forms": "npm:@angular/forms/bundles/forms.umd.js",
"rxjs": "npm:rxjs",
"angular-in-memory-web-api": "npm:angular-in-memory-web-api"
},
"packages": {
"app": {
"main": "./main.js",
"defaultExtension": "js"
}
}
我的tsconfig.json看起来像这样:
{
"compilerOptions": {
"target": "es6",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"typeRoots": [ "node_modules" ],
"types": [ "system" ]
}
}
但是我在VS中的“ TypeScript build”选项卡与此并不太一致(尽管存在tsconfig,但仍可编辑):
现在,如果我将下拉菜单从CommonJS更改为System,则会遇到成百上千个错误,TS编译无法找到模块“ X”,而且我不清楚如何解决这些错误-我似乎所发现的都是古老的帖子(仍指Angular 作为Angular2)和无法解决的建议。
我的VS打字稿构建设置是否需要与tsconfig.json对齐?我当前是否遇到问题,因为tsc正在注意VS设置并生成与tsconfig中设置的systemjs不兼容的东西/正在客户端模块加载中使用?