2
votes

I'm trying to set up an express web server using TypeScript, but get this error every time I attempt to transpile to JS

error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.

I'm a newbie, and this is what I've done (using WebStorm). I installed the following express ambient dependency using typings, i.e.,

typings install --ambient --save express

which created the following typings.json file

{
  "ambientDependencies": {
    "express": "registry:dt/express#4.0.0+20160317120654",
  }
}

looking at the index.d.ts file in typings\main\ambient\express\, WebStorm complains that it can't find the modules serve-static, express-serve-static-core and mime. So I installed those, along with node, such that typings.json becomes

{
  "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"
  }
} 

I then added a the following tsconfig.json file

{
  "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"
  ]
}

I installed the express module from npm such that my package.json looks like

{
  "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"
}

and created server.ts as follows

/** Created by James on 5/12/2016. */
/// <reference path="typings/main.d.ts"/>
import express = require('express');
var app = express();

Now attempting to transpile this using tsc server.tsI get the following errors

\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.

I'm obviously missing something, but can't figure it out. Any advice please?

1
The errors you are listing are on line 4. Can you post your entire server.ts file? - rgvassar
Edited the question adding the top two lines to server.ts - James B
Letting WebStorm do the compilation seems to work. But typing tsc server.ts at the command line throws the errors shown above. - James B
typing tsc -v at the command line reports a version number of 1.0.3.0. WebStorm appears to use a bundled version 1.8.7. Are these different compilers? - James B
Yes they are different. Definitely update typescript. Version 1.0.3 is almost 2 years old. - rgvassar

1 Answers

0
votes

I hadn't realised that there was an existing install of Typescript with the Microsoft SDK for Visual Studio. Removing C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\ from the system environment variables enabled me to use the later version of the typescript compiler installed via node.