GDAX-tt中TypeScript报Unexpected token import错误求解决方案
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-styleimportstatements torequire()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 likegdax-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
--projectflag 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.jsonis not being applied correctly. Make sure it's in your project root, and runtsc --project ./tsconfig.json ./tutorials/t001_feeds.tsto 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

