最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript, SyntaxError: Unexpected token { - Stack Overflow

programmeradmin3浏览0评论

I am trying to run my program using "node main.js", however, it keeps ing up with the error "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 './likeponent';
       ^

SyntaxError: Unexpected token {
    at Module._pile (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)

I have tried changing the tsconfig.json file to "module": "monjs", however, this did not work. I have even uninstalled and reinstalled node and started from scratch.

import{LikeComponent} from './likeponent';

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

It should output the program correctly onto the mand prompt.

I am trying to run my program using "node main.js", however, it keeps ing up with the error "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.ponent';
       ^

SyntaxError: Unexpected token {
    at Module._pile (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)

I have tried changing the tsconfig.json file to "module": "monjs", however, this did not work. I have even uninstalled and reinstalled node and started from scratch.

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

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

It should output the program correctly onto the mand prompt.

Share Improve this question asked Jun 11, 2019 at 14:57 GolfBravoGolfBravo 1,1272 gold badges14 silver badges28 bronze badges 2
  • 1 Use the piler option "module": "monjs" in tsconfig.json. Then pile again. – Paleo Commented Jun 11, 2019 at 15:06
  • 1 Can you please clarify what you mean by the piler option? I have already gone into the config file and changed module to "module": "monjs", – GolfBravo Commented Jun 11, 2019 at 15:57
Add a ment  | 

4 Answers 4

Reset to default 7

Notice that your are running node main.js.

Your main.js currently has esm syntax (aka ES6 module syntax).

Assuming it is piled from main.ts, you need to have module: monjs in your `tsconfig.json

// tsconfig.json
{
  "pilerOptions": {
    "module": "monjs",
    ... // other configurations
  }
}

With that, when you pile your code by running tsc, it will create main.js with monjs module syntax.

If you are trying to debug a vscode project and you already have "module": "monjs", then check your "launch.json" outFiles to make sure it aligns with where your output files actually are (typically set via outDir):

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft./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
    }
  ]
}

It seems like if vscode doesn't find a matching file, it just attempts to run the ts file via node (which explains the error).

I just thrashed on this for a while, so hopefully this saves someone else some headache.

These kinds of errors crop up because of syntax differences between versions of Javascript/ECMAScript. They can be avoided by using the configurations found in the @tsconfig/bases github repository which are remended by the TypeScript team. Just make sure to use the corresponding tsconfig for the environment the Javascript is intended to run in.

The TSConfig Bases are available for install on NPM. Install and then extend the @tsconfig/node12/tsconfig.json like so:

npm install --save-dev @tsconfig/node12

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

My issue stemmed from problems installing all of the required packages from npm. I deleted my node_modules folder and used yarn to install again, and then yarn install --check-files to ensure they were installed correctly.

发布评论

评论列表(0)

  1. 暂无评论