3

I'm trying to learn TypeScript and Angular, but there's a strange error that keeps bugging me.

In short, I import a class named Point from the module point. The error suggests that a curly brace is wrong. The error points to the import statement in main.ts (line number 1).

Furthermore, I have the .ts and .js files in a folder. This folder does not have a tsconfig.json file.

I transpile and run the code with:

tsc main.ts --target ES2016 && node main.js

I target ES2016 for the support of getter and setter properties.

error

main.js:1
(function (exports, require, module, __filename, __dirname) { import { Point } from './point';

SyntaxError: Unexpected token {
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)

main.ts

import { Point } from './point';

let point = new Point(1);
point.draw();
let x = point.x;
console.log(x);
point.y = 500;
point.draw();

point.ts

export class Point {
    constructor(private _x?: number, private _y?: number) { }

    get x() {
        return this._x;
    }

    set y(value) {
        if (value < 0)
            throw new Error('New X must be 0 or higher.');

        this._y = value
    }

    draw() {
        console.log('X: ' + this._x + ', Y: ' + this._y);
    }
}
12
  • what's the error message? what you linked seems to be a bit of the transpiled code? Most likely since you are using modules you will need to get a module loader as well to run the code, you can't run it without. Commented Jul 28, 2018 at 19:15
  • actually I take that back, on node it should run I guess but the transpilation looks weird. Maybe you need to set module type in tsconfig.json to node instead of what you have now. Commented Jul 28, 2018 at 19:17
  • Sorry about that, forgot the actual error message. See my post above Commented Jul 28, 2018 at 19:17
  • In the console, also an arrow is pointing to the { in: import { Point Commented Jul 28, 2018 at 19:20
  • 1
    @Tim it is related to node version, you have to use to run with --harmony flag Commented Jul 28, 2018 at 20:15

1 Answer 1

3

this tsconfig.json option.

{
   "compilerOptions": {
    "target": "ES2016",
    "module": "commonjs",
    "outDir": "./dest",
    "strict": true,
    "esModuleInterop": true
  }
}

and I have compile like this tsc --p tsconfig.json and everything seem working fine.

typescript compile

typescript version : Version 2.9.2 node version : v10.2.0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.