【问题标题】:Express server using TypeScript and Typings使用 TypeScript 和 Typings 的 Express 服务器
【发布时间】: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-staticexpress-serve-static-coremime。所以我安装了这些,连同节点,这样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


【解决方案1】:

我没有意识到已经安装了带有 Microsoft SDK for Visual Studio 的 Typescript。从系统环境变量中删除C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\ 使我能够使用通过节点安装的更高版本的打字稿编译器。

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2017-04-14
    • 2017-03-19
    • 2016-12-17
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多