TypeScript: Unexpected reserved word

Alon

I have a project in WebStorm IDE & Node.js. I created a file named "mongo-manager.ts" which I used in the routes\index.js file. It worked seamlessly.

Then I did 2 things:

  1. WebStorm had always asked me whether or not to compile TypeScript to JavaScript and I had always ignored it, but then once I clicked on "yes" and since then after solving some errors it's compiling now ts files successfully to js.
  2. I converted index.js into index.ts and now I still have index.js which is an output of the compilation.

Ever since then, when I try to run my app (using the triangular green button in WebStorm which in turn uses the command line: "C:\Program Files (x86)\JetBrains\WebStorm 11.0.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" bin\www), I get this error:

import mongodb = require('mongodb');

^^^^^^

SyntaxError: Unexpected reserved word

Now, there's no specific problem with the "import" statement because if I omit it, the next non JavaScript statement throws the same exception. It just tries to treat TypeScript like it was JavaScript.

I've found these questions: Typescript: unexpected reserved word in PHPStorm, Unable to import in typescript file in nodejs

They suggest either to use "tsc" rather than node.exe, or to make sure that I run with node.exe the js file rather than the ts file.

Now the thing is that the file that node.exe runs directly is "www" which is a JavaScript file and in turn calls app.js which is still JavaScript, but later the files index.ts and mongo-manager.ts are called, so I have a hybrid of both js and ts.

And another and most important thing is that my code has already worked seamlessly even though it's hybrid and without taking any of the suggestions in these questions, until I answered WebStorm "yes" to the question whether to compile TypeScript to JavaScript, which had always appeared.

Anyway, I only want my app working again, no matter how.

Any help will be profoundly appreciated!

vilicvane

You need to know differences between compilation error and runtime error. It seems that what you've experiencing is a runtime error. Please check out the JavaScript file being executed by Node.js.

Currently Node.js does not yet support ES6 module syntax, you need to have your module field in tsconfig.json to be commonjs. Ideally in your output JavaScript file, import mongodb = require('mongodb'); should be emitted as const mongodb = require('mongodb'); when targeting ES6 or var mongodb = require('mongodb'); when targeting ES5.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected reserved word 'import' when using babel

unexpected reserved word import in node.js

SyntaxError: Unexpected reserved word on typescript

What is the "type" reserved word in TypeScript?

Can I ignore Unexpected reserved word error by eslint-loader

Unexpected use of reserved word 'let' in strict mode

TypeScript error: Reserved word 'this' used as name

Meteor 1.3 NPM: Unexpected reserved word import

SyntaxError: Unexpected reserved word on running mocha with enzyme

Node.js application SyntaxError: Unexpected reserved word 'class'

Is `Account` a reserved word in TypeScript?

unexpected behavior by reduceByKey in spark(with scala) ? Is "rev" reserved word in spark or scala?

SyntaxError: Unexpected reserved word, for await loop

Parsing error: Unexpected reserved word 'static'

SyntaxError: Unexpected reserved word => prettier/third-party.js

Unexpected reserved word error using Jest

`export default let foo = ...` throws "Unexpected strict mode reserved word"

Jint "unexpected reserved word"

Jest test of React application with TypeScript SyntaxError... Unexpected reserved word 'interface'

Unexpected reserved word 'await'

Unexpected reserved word 'await' in async function in react

SyntaxError: unexpected reserved word 'await' in async function

Unexpected reserved word 'await' in getting the session id

React JS Unexpected reserved word 'await'

Unexpected reserved word 'await' for react native

Firestore: Uncaught SyntaxError: Unexpected reserved word

SyntaxError: Unexpected reserved word "await"

Uncaught SyntaxError: Unexpected reserved word in a {name} file

Typescript - exclude reserved word from string