我正在研究“为 .NET 开发人员学习 Angular”一书中的项目。 ch.3 中有两个。我对项目代码本身没有问题。问题是我无法让项目运行。在 typescript 转译过程中构建失败。
我正在使用 npm start
命令调用代码。这是 package.json 文件的脚本部分:
"name": "angular-io-example",
"version": "1.0.0",
"scripts": {
"test:once": "karma start karma.conf.js --single-run",
"build": "tsc -p src/",
"serve": "lite-server -c=bs-config.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"build:watch": "tsc -p src/ -w",
"build:upgrade": "tsc",
"serve:upgrade": "http-server",
"build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup-config.js",
"serve:aot": "lite-server -c bs-config.aot.json",
"build:babel": "babel src -d src --extensions \".es6\" --source-maps",
"copy-dist-files": "node ./copy-dist-files.js",
"i18n": "ng-xi18n",
"lint": "tslint ./src/**/*.ts -t verbose"
}
(不幸的是,这本书没有解释 package.json 文件——它只是作为可下载的内容提供给您。)
问题出现在构建命令"build": "tsc -p src/"
。
当我尝试按原样运行它时,我在命令行界面中得到以下信息:
>tsc -p src/
node_modules/@types/angular/index.d.ts(232,41): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(233,41): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1252,41): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1253,41): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1972,55): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1976,54): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1986,55): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(1991,57): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(2005,48): error TS1005: ',' expected.
node_modules/@types/angular/index.d.ts(2088,22): error TS1005: ',' expected.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! angular-io-example@1.0.0 build: 'tsc -p src/'
npm ERR! Exit status 2
我不确定 node_modules 错误是否是问题所在,因为我可以使用 tsc 手动编译 typescript 代码。我收到相同的错误消息,但代码可以编译。 (我也对为什么转译器引用 node_module 代码感到困惑,因为它在 tsconfig.json 文件中设置为“排除”。)
然而,问题似乎出在 tsc 命令上。我发现如果我用一些愚蠢的东西替换构建命令,比如“ipconfig”,项目运行完美(因为 typescript 代码是手动转译的)。
我希望有人能向我解释构建过程失败的原因。
最佳答案
问题确实是 devDependencies Node 引入了旧版本的 tsc 库,并使用它而不是全局版本的 tsc。这可能就是 npm update 命令没有帮助的原因。另外rxjs的版本有问题,需要更新。现在一切正常。
感谢大家的评论。当您想学习一些东西但由于一些愚蠢的问题而无法到达一垒时,这真是一种拖累
关于node.js - Angular/TypeScript 编译的初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916037/