【发布时间】:2016-09-08 18:55:07
【问题描述】:
我正在尝试使用 TypeScript 设置快速 Web 服务器,但每次尝试转换为 JS 时都会出现此错误
error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.
我是新手,这就是我所做的(使用 WebStorm)。我使用类型安装了以下快速环境依赖项,即,
typings install --ambient --save express
它创建了以下 typings.json 文件
{
"ambientDependencies": {
"express": "registry:dt/express#4.0.0+20160317120654",
}
}
查看typings\main\ambient\express\ 中的index.d.ts 文件,WebStorm 抱怨它找不到模块serve-static、express-serve-static-core 和mime。所以我安装了这些,连同节点,这样typings.json 就变成了
{
"ambientDependencies": {
"express": "registry:dt/express#4.0.0+20160317120654",
"express-serve-static-core": "registry:dt/express-serve-static-core#0.0.0+20160322035842",
"mime": "registry:dt/mime#0.0.0+20160316155526",
"node": "registry:dt/node#4.0.0+20160509154515",
"serve-static": "registry:dt/serve-static#0.0.0+20160501131543"
}
}
然后我添加了以下tsconfig.json 文件
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
],
"files": [
"server.ts"
]
}
我从 npm 安装了 express 模块,这样我的 package.json 看起来像
{
"name": "using-typings",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.13.4",
"mime": "^1.3.4"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
并如下创建server.ts
/** Created by James on 5/12/2016. */
/// <reference path="typings/main.d.ts"/>
import express = require('express');
var app = express();
现在尝试使用tsc server.ts转译它,我收到以下错误
\using-typings>tsc server.ts
\using-typings\server.ts(4,1): error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.
\using-typings\server.ts(4,8): error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.
\using-typings\server.ts(4,13): error TS1005: ';' expected.
\using-typings\server.ts(4,21): error TS1005: ';' expected.
\using-typings\server.ts(4,26): error TS1005: ';' expected.
我显然错过了一些东西,但无法弄清楚。请问有什么建议吗?
【问题讨论】:
-
您列出的错误在第 4 行。您可以发布整个 server.ts 文件吗?
-
编辑了将前两行添加到 server.ts 的问题
-
让 WebStorm 进行编译似乎可行。但是在命令行输入
tsc server.ts会抛出上面显示的错误。 -
在命令行输入
tsc -v报告版本号为 1.0.3.0。 WebStorm 似乎使用捆绑的 1.8.7 版本。这些是不同的编译器吗? -
是的,它们是不同的。绝对更新打字稿。 1.0.3 版已经快 2 年了。
标签: node.js express typescript