Ts-node: How to debug SyntaxError: Unexpected token {

Created on 11 Apr 2016  路  10Comments  路  Source: TypeStrong/ts-node

I'm trying to convert an es6 project over to typescript. However, I'm getting the following error and I don't know how to go about debugging it as there are no files or line numbers emitted:

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.loader (/usr/local/lib/node_modules/ts-node/src/ts-node.ts:224:14)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-node/src/bin/ts-node.ts:110:12)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)

Any suggestions for debugging?

Most helpful comment

No, there shouldn't be any ts files yet, I wanted to slowly convert the project over. So I was trying to see if I could just get the es6 to run first.

This is my tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": false,
        "allowJs": true
    },
    "exclude": [
        "node_modules"
    ]
}

I've literally just installed ts-node and added a tsconfig.json and tried to run it. Not sure if it's a valid use case, but I'm trying to find an easy way to transition a project from es6 to typescript.

All 10 comments

Hmm, are you importing from a JavaScript file or the CLI?

I'm using the cli. Basically:

ts-node main.js

(my tsconfig.json has allowJs set to true)

Are any of the files .ts? This appears to be coming from a .ts file - which appears to be really odd. TypeScript is compiling it, but then it's failing to actually execute. Are you compiling for ES6 at all?

No, there shouldn't be any ts files yet, I wanted to slowly convert the project over. So I was trying to see if I could just get the es6 to run first.

This is my tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": false,
        "allowJs": true
    },
    "exclude": [
        "node_modules"
    ]
}

I've literally just installed ts-node and added a tsconfig.json and tried to run it. Not sure if it's a valid use case, but I'm trying to find an easy way to transition a project from es6 to typescript.

Ok, I see. Was it running on node before, or were you using Babel? The odd thing here is that ts-node doesn't even do anything on non-.ts files, so nothing here should be going through TypeScript at all.

Yeah, just running it on node.

Ha, that is odd, yeah, no typescript whatsoever at this point.

I tried changing my main.js to main.ts and running it, but it imports other js files obviously and I end up seeing the same error SyntaxError: Unexpected token {.

To confirm, everything works fine when you do node main.js?

Good catch! When I run the app, I'm doing electron main.js (it's an electron app), running node main.js throws:

/Users/doug/Projects/inofile/kno2-electron/main.js:4
const { SocketHandler, IpcHandler, SquirrelHandler } = require('./lib/handlers');
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3

Which is the error we're seeing. Apparently this is a non issue, ha ha. Thanks for your time. :+1:

Was facing the same error, in my case it was because ts-node was picking up the tsconfig.json from current directory and not the one I was expecting, cause of that it compiled the script into esm which does not work on node without the flag.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dakom picture dakom  路  3Comments

JoseLion picture JoseLion  路  3Comments

remojansen picture remojansen  路  4Comments

cevek picture cevek  路  4Comments

grissius picture grissius  路  3Comments