ts-node: неожиданный токен ':' - PullRequest
0 голосов
/ 26 января 2020

Я пишу сервер для своего приложения и решил изменить babel-node executor на ts-node . Моя команда в пакете . 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)

Я пытался скомпилировать небольшой файл, но компилятор не понимает машинопись.

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)

Я уже переустановил * Модули 1023 * ts-node и * .

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": ["**/*"]
}

Ответы [ 2 ]

0 голосов
/ 27 января 2020

Нашел решение. В конфигурации проекта (файл пакет. json) необходимо было убрать модульный тип работы JS.

Строка: "type": "module",

0 голосов
/ 26 января 2020

Попробуйте переименовать: Server.js в Server.ts

...