1

I have one class defined in separate file named spartan.ts and here it is how it looks:

class Spartan {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
    test() {
        return this.name;
    }
}

module.exports = Spartan;

Then I am importing this to other file which looks like this:

var Spartan = require("../entities/spartan.ts");
var mySpartan = new Spartan("myName");
console.log(mySpartan.test())

My tsconfing.json looks like this:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

And than I get this error:

SyntaxError: Unexpected token :
        at createScript (vm.js:80:10)
        at Object.runInThisContext (vm.js:139:10)
        at Module._compile (module.js:616:28)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (/U.../routeRepository.ts:2:15)
2
  • can you show the compiled .js file? Commented Sep 18, 2018 at 11:31
  • 1
    Looking at the exception stack trace, it looks like the require call is actually expecting a vanilla JavaScript file, and it fails because of the TypeScript type annotations. How do you build the project? Commented Sep 18, 2018 at 11:51

1 Answer 1

1

You should probably use the ES2015 module syntax for importing/exporting instead, for example:

export class Spartan {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
    test() {
        return this.name;
    }
  }

And then:

import { Spartan } from "../entities/spartan.ts";
let mySpartan = new Spartan("myName");
console.log(mySpartan.test())
Sign up to request clarification or add additional context in comments.

7 Comments

Its just export class
Yeah, that's more concise. Updated. Thanks @Antoniossss.
I think export is not a problem since if I change a code and run it like this it is working: class Spartan { test() { return "My name is"; } } module.exports = Spartan;
That's probably because there is no TypeScript syntax. In your original example, you're trying to require a TypeScript file which won't work because require expects a JavaScript file.
However, if I drop the constructor and variables declaration in Spartan class, it consoles log "My name is"'.
|

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.