Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij Typescript error for interface: SyntaxError: Unexpected identifie

I'm trying to perform a simple test of an interface. The code is as follows:

interface TestInterface {
    id: number;
    text: string;
}

const testInterfaceImplementation: TestInterface = {
    id: 1,
    text: 'sample text'
};

console.log(testInterfaceImplementation.text);

When I run this code with Node.js configuration I get this error:

interface TestInterface {
          ^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

When I run the code without this interface it works fine:

const testInterfaceImplementation = {
    id: 1,
    text: 'sample text'
};

console.log(testInterfaceImplementation.text);

What is the problem? I've also tried to move the interface to a different .ts file but the error still occurs.

tsconfig.json file:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

run_configuration

like image 628
Chris_T Avatar asked Nov 19 '25 20:11

Chris_T


1 Answers

You can't run the Typescript code by passing it to Node.js directly, Node.js doesn't provide native support for executing Typescript. The code has to be either compiled on-the-fly or precompiled. here are some recipes:

To run a selected TypeScript file using ts-node:

  • Install ts-node using npm i ts-node.
  • Create a new Node.js run/debug configuration.
  • Add --require ts-node/register to the Node parameters field.
  • In the JavaScript file field add $FilePathRelativeToProjectRoot$.
  • Save configuration.
  • Use it to run (or debug) a file that is currently opened in the editor or selected in the Project view. You can do that using the icons on the navigation bar or Run... action.

If you need to pass any additional parameters to ts-node (e.g. --project tsconfig.json), you can add them to the Application parameters field in the run/debug configuration.

To compile app with TypeScript and run a selected TypeScript file

  • Create a Node.js run/debug configuration.
  • In the Before Launch section, click Add and select Compile TypeScript.
  • Select tsconfig.json.
  • In the JavaScript file field you need to select the path to the compiled .js file.
  • If a compiled JavaScript lives next to its source, add $FileRelativeDir$/$FileNameWithoutExtension$.js
  • If files are saved in an output folder (preserving the folder structure), add the folder name before the pattern, e.g. build/$FileRelativeDir$/$FileNameWithoutExtension$.js
  • Save configuration.
  • Use it to run (or debug) a file that is currently opened in the editor or selected in the Project view.
like image 124
lena Avatar answered Nov 22 '25 08:11

lena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!