【发布时间】:2017-12-21 00:00:39
【问题描述】:
我正在研究“为 .NET 开发人员学习 Angular”一书中的项目。第 3 章中有两个。我对项目代码本身没有问题。问题是我无法让项目运行。在打字稿转译过程中构建失败。
我正在使用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 手动编译打字稿代码。我收到相同的错误消息,但代码可以编译。 (我也很困惑为什么转译器会引用 node_module 代码,因为它在 tsconfig.json 文件中设置为“排除”。)
但问题似乎出在 tsc 命令上。我发现如果我用一些愚蠢的东西代替构建命令,比如“ipconfig”,项目运行完美(因为打字稿代码是手动转译的)。
我希望有人可以向我解释构建过程失败的原因。
【问题讨论】:
-
你使用的是什么版本的打字稿?
-
我强烈建议您改为使用 Angular CLI 来学习 Angular。 Angular CLI 在 Angular 之后几个月问世,所以许多原始书籍/课程都没有展示如何使用它。 Angular CLI 现在是构建 Angular 应用程序的 方式,并且易于使用。您可以在此处找到分步教程:angular.io/tutorial/toh-pt0
-
我使用的是 typescript 2.6.2 版。希望它是当前版本 - 我是几天前才下载的。
-
其实,我刚刚注意到一些事情。 package.json 文件引入了一个打字稿转译器,它的版本是 2.2+。大概是被调用的版本,而不是我的全局安装。
标签: node.js angular typescript