@EnDronist

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

Я пишу backend-сервер для своего приложения и решил сменить исполнителя с babel-node на ts-node. Моя команда в package.json:
"server": "cd server && ts-node --project tsconfig.json -r ts-node/register Server.ts",

Но получаю одну и ту же ошибку:
> nodejs-server@1.0.0 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)

Я пытался запустить небольшой файл, но компилятор не понимает typescript'а.
test.ts:
var test: string = 'Hello!';
console.log(test);

Я получил такую же ошибку:
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)

Я уже переустанавливал модули ts-node и typescript.
Корневой 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": ["**/*"]
}
  • Вопрос задан
  • 2733 просмотра
Решения вопроса 1
@EnDronist Автор вопроса
Нашёл решение.
В конфигурации проекта (файл package.json) нужно было убрать модульный тип работы JS.
Строка: "type": "module",
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы