【问题标题】:ts-node: Unexpected token ':'ts-node:意外的令牌':'
【发布时间】:2020-05-11 23:18:39
【问题描述】:

我正在为我的应用程序编写后端服务器,并决定将 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-nodetypescript 模块。

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

【问题讨论】:

    标签: node.js typescript ts-node


    【解决方案1】:

    如果您有 tsconfig,请确保没有将任何内容设置为“ESNext”。这可以是 targetmodule 参数。

    如果从命令行运行它,试试这个:

    "ts-node -O '{\"module\": \"commonjs\", \"target\": \"ES6\" }' ./path/to/MyFile.ts"

    如果这不起作用,请放弃 ts-node 并使用 tsc & node ./dist/MyFile.js

    【讨论】:

      【解决方案2】:

      找到了解决办法。 在项目配置(文件package.json)中,需要移除模块化类型的工作JS。

      线路:"type": "module",

      【讨论】:

        【解决方案3】:

        尝试重命名:Server.jsServer.ts

        【讨论】:

          猜你喜欢
          • 2016-01-20
          • 2017-05-11
          • 1970-01-01
          • 2021-10-03
          • 1970-01-01
          • 2021-03-19
          • 2021-09-05
          • 2020-05-22
          • 2017-09-26
          相关资源
          最近更新 更多