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

TypeScript项目启动报unexpected token import错误排查求助

Fixing "Unexpected Token Import" When Using TypeScript, GruntJS, TypeORM & typeorm-model-generator

Hey there, let's break down what's going on here—this is a super common gotcha when mixing TypeScript, build tools, and database ORMs like TypeORM. The core issue is clear: your app is trying to execute raw TypeScript files (which use import syntax Node.js doesn't natively understand) instead of the compiled JavaScript in your dist folder, and this only pops up when you add database code because that's when TypeORM starts loading entity/model files.

Here are the most likely culprits to check:

1. Your TypeORM configuration points to TypeScript source files instead of compiled JS

TypeORM relies on a config file (like ormconfig.json or ormconfig.ts) to find entities, migrations, and subscribers. If you used typeorm-model-generator to create models in your src/entities folder, it's easy to forget to update the config paths to point to the compiled versions in dist.

Bad Config Example (points to TS files):

{
  "type": "postgres",
  "host": "localhost",
  "entities": ["src/entities/**/*.ts"], // ❌ Wrong path
  "migrations": ["src/migrations/**/*.ts"] // ❌ Wrong path
}

Fixed Config Example (points to compiled JS):

{
  "type": "postgres",
  "host": "localhost",
  "entities": ["dist/entities/**/*.js"], // ✅ Correct path
  "migrations": ["dist/migrations/**/*.js"] // ✅ Correct path
}

If you're using a TypeScript config file (ormconfig.ts), make sure it compiles to a JS file in dist and that the paths inside it reference the dist folder, not src.

2. Your GruntJS build isn't compiling all TypeScript files

If your Grunt task only compiles your entry file (like src/index.ts) but misses the entity/model files generated by typeorm-model-generator, those files won't exist in dist. When TypeORM can't find the compiled JS, it might fall back to reading the raw TS files, triggering the import error.

Check your Gruntfile.js to ensure your TypeScript task includes all .ts files in src:

grunt.initConfig({
  ts: {
    default: {
      src: ["src/**/*.ts"], // ✅ Includes all TS files in src and subfolders
      dest: "dist",
      options: {
        module: "commonjs", // Critical for Node.js compatibility
        target: "es6",
        outDir: "dist",
        rootDir: "src"
      }
    }
  }
});

Run your build command (e.g., grunt ts) and verify that dist/entities has the corresponding .js files for every .ts model in src/entities.

3. You're starting the app with a TypeScript file instead of the compiled JS

Double-check how you're launching your app. If you're running something like node src/index.ts, Node will try to execute the raw TS file directly (which throws the import error). You need to run the compiled JS in dist instead.

Update your package.json scripts to ensure you build first, then run the correct file:

{
  "scripts": {
    "build": "grunt ts",
    "start": "node dist/index.js" // ✅ Runs compiled JS
  }
}

Always run npm run build before npm start to make sure dist is up to date.

4. Hardcoded paths in your connection initialization

If you're manually creating a TypeORM connection in your code (instead of relying on ormconfig), you might have hardcoded paths that point to TS files. For example:

Bad Initialization:

import { createConnection } from "typeorm";

await createConnection({
  entities: [__dirname + "/entities/*.ts"], // ❌ References TS files
  // ... other config
});

Fixed Initialization:

import { createConnection } from "typeorm";

await createConnection({
  entities: [__dirname + "/entities/*.js"], // ✅ References compiled JS
  // ... other config
});

Or better yet, let TypeORM load the config from your ormconfig.json to avoid hardcoding paths entirely:

await createConnection(); // Automatically uses ormconfig.json

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:50:40