You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDAX-tt中TypeScript报Unexpected token import错误求解决方案

Solution for "SyntaxError: Unexpected token import" with ts-node and GDAX-tt

Let's break down why you're hitting this error and how to fix it step by step. The Unexpected token import issue here almost always boils down to ts-node not properly compiling your TypeScript file to a module format Node.js understands, usually because of misconfigured tsconfig.json or missing runtime flags.

Step 1: Validate Your tsconfig.json Core Settings

First, make sure your tsconfig.json (in your project root) has these critical settings—they tell TypeScript how to transpile your code for Node.js:

  • "module": "commonjs": Node.js uses CommonJS modules by default, so TypeScript needs to convert ES-style import statements to require() calls.
  • "target": "es6" or higher: Ensures compatibility with modern JS syntax that Node.js 9.x supports.
  • "moduleResolution": "node": Lets TypeScript resolve modules the same way Node.js does, which is essential for importing packages like gdax-trading-toolkit.

A minimal working tsconfig.json for your setup might look like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./build"
  },
  "include": [
    "./src/**/*",
    "./tutorials/**/*"
  ]
}

Note the include section—make sure it covers your tutorial files so TypeScript processes them.

Step 2: Verify ts-node is Reading Your tsconfig

Sometimes ts-node doesn't pick up the tsconfig.json automatically (especially if you're running the script from a subdirectory). To check what configuration ts-node is using, run:

ts-node --showConfig ./tutorials/t001_feeds.ts

Look for the module and target values in the output. If they don't match your tsconfig.json, explicitly specify the config path when running your script:

ts-node --project ./tsconfig.json ./tutorials/t001_feeds.ts

Step 3: Test Compilation Outside ts-node

To isolate whether the issue is with ts-node or your TypeScript setup, compile the tutorial file directly with tsc:

tsc ./tutorials/t001_feeds.ts

This will generate a .js file in the same directory. Try running it with Node:

node ./tutorials/t001_feeds.js
  • If this runs without errors: The problem is specific to ts-node's runtime compilation. Double-check the --project flag or try reinstalling ts-node to ensure no corrupted files: npm install -g ts-node@4.1.0.
  • If this still throws the import error: Your tsconfig.json is not being applied correctly. Make sure it's in your project root, and run tsc --project ./tsconfig.json ./tutorials/t001_feeds.ts to force it to use your config.

Step 4: Check Local vs Global Package Conflicts

Global installations of ts-node or TypeScript can sometimes clash with local project versions. Try installing both packages locally to your project to avoid this:

npm install --save-dev typescript@2.6.2 ts-node@4.1.0

Then run your script using the local ts-node instance:

npx ts-node ./tutorials/t001_feeds.ts

Quick Note on Why gdaxConsole.js Worked

That script in the build directory is already a compiled CommonJS module—its import statements have been converted to Node-friendly require() calls during the build process. The tutorial .ts files, however, need proper runtime transpilation from ts-node to make their syntax compatible with Node.js.

内容的提问来源于stack exchange,提问作者user40176

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:08:13