【问题标题】:TypeScript, SyntaxError: Unexpected token {打字稿,语法错误:意外的令牌{
【发布时间】:2019-10-26 01:02:47
【问题描述】:

我正在尝试使用“node main.js”运行我的程序,但是,它不断出现错误“SyntaxError: Unexpected token {”

D:\Visual Studio Code Projects\ts-hello>node main.js
D:\Visual Studio Code Projects\ts-hello\main.js:1
import { LikeComponent } from './like.component';
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787: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:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

我尝试将 tsconfig.json 文件更改为“module”:“commonjs”,但是,这不起作用。我什至卸载并重新安装了节点并从头开始。

import{LikeComponent} from './like.component';

let component = new LikeComponent(10, true);
component.onClick();
console.log(`likesCount: ${component.likesCount}, isSelected: ${component.isSelected}`);

它应该正确地将程序输出到命令提示符。

【问题讨论】:

  • tsconfig.json 中使用编译器选项"module": "commonjs"。然后再次编译。
  • 您能否澄清一下您所说的编译器选项是什么意思?我已经进入配置文件并将模块更改为“模块”:“commonjs”,

标签: javascript typescript ecmascript-6


【解决方案1】:

由于 Javascript/ECMAScript 版本之间的语法差异,会出现这类错误。它们可以通过使用@tsconfig/bases github repository 中的配置来避免,它们是recommended by the TypeScript team。只需确保为 Javascript 打算运行的环境使用相应的 tsconfig。

TSConfig Bases 可用于安装在 NPM 上。像这样安装并扩展@tsconfig/node12/tsconfig.json:

npm install --save-dev @tsconfig/node12

{
  "extends": "@tsconfig/node12/tsconfig.json",
  "compilerOptions": {
    ...
  },
  ...
}

【讨论】:

    【解决方案2】:

    如果您正在尝试调试 vscode 项目并且您已经拥有 "module": "commonjs",,请检查您的“launch.json”outFiles 以确保它与您的输出文件的实际位置对齐(通常通过 outDir 设置) :

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch Program",
          "program": "${workspaceFolder}/src/index.ts",
          "preLaunchTask": "tsc: build - tsconfig.json",
          "outFiles": ["${workspaceFolder}/build/**/*.js"], //make sure this matches!
          "sourceMaps": true
        }
      ]
    }
    

    似乎如果 vscode 没有找到匹配的文件,它只是尝试通过节点运行 ts 文件(这解释了错误)。

    我只是在这个问题上纠结了一段时间,所以希望这可以避免其他人头疼。

    【讨论】:

      【解决方案3】:

      请注意,您正在运行node main.js

      您的 main.js 当前具有 esm 语法(也称为 ES6 模块语法)。

      假设它是从main.ts 编译的,你需要在你的`tsconfig.json 中有module: commonjs

      // tsconfig.json
      {
        "compilerOptions": {
          "module": "commonjs",
          ... // other configurations
        }
      }
      

      这样,当您通过运行 tsc 编译代码时,它将使用 commonjs 模块语法创建 main.js

      【讨论】:

        猜你喜欢
        • 2017-01-16
        • 2017-05-02
        • 2019-08-07
        • 1970-01-01
        • 2018-12-20
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        相关资源
        最近更新 更多