E
E
EnDronist2020-01-26 11:51:05
Node.js
EnDronist, 2020-01-26 11:51:05

Node.js - ts-node: Unexpected token ':'?

I am writing a backend server for my application and decided to change the executor from babel-node to ts-node . My command in package.json :

"server": "cd server && ts-node --project tsconfig.json -r ts-node/register Server.ts",

But I get the same error:
> [email protected] server D:\Projects\Visual Studio Code\NodeJS Server
> cd server && ts-node --project tsconfig.json -r ts-node/register Server.ts

(node:3824) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token ':'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
    at link (internal/modules/esm/module_job.js:36:21)

I tried to run a small file, but the compiler doesn't understand typescript.
test.ts :
var test: string = 'Hello!';
console.log(test);

I got the same error:
PS D:\Projects\Visual Studio Code\NodeJS Server\server> npx ts-node test.ts
(node:1420) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token ':'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
    at link (internal/modules/esm/module_job.js:36:21)

I have already reinstalled the ts-node and typescript modules .
Root tsconfig.json:

{
    "compilerOptions": {
        "target": "ES6",
        "moduleResolution": "Node",
        "traceResolution": false,
        "allowJs": false,
        "esModuleInterop": true,
        "declaration": false,
        "noResolve": false,
        "noImplicitAny": false,
        "removeComments": true,
        "strictNullChecks": false,
        "sourceMap": false,
        "skipLibCheck": true,
        "resolveJsonModule": true,
        "typeRoots": [
            "custom_typings",
            "node_modules/@types",
        ],
    },
    "include": [
        "client",
        "server",
    ],
    "exclude": [
        "node_modules",
        "client/bundle.js"
    ]
}


server/tsconfig.json:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "module": "CommonJS",
        "jsx": "preserve",
        "noEmit": true,
        "baseUrl": "../",
        "paths": {
            "*": ["node_modules/*"],
            "@server/*": ["server/*"],
            "@client/*": ["client/*"],
            "@routes/*": ["server/routes/*"],
            "@public/*": ["public/*"],
            "@secure/*": ["secure/*"],
            "@utils/*": ["utils/*"],
        },
    },
    "include": ["**/*"]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EnDronist, 2020-01-27
@EnDronist

Found a solution.
In the project configuration (file package.json ) it was necessary to remove the modular type of JS work.
Line:"type": "module",

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question