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

如何运行调用NestJS服务的外部脚本?

问题

在src目录外运行调用Nest独立应用的脚本时,已通过tsconfig.json添加以下配置修复大部分导入错误:

"ts-node": {
  "require": ["tsconfig-paths/register"]
}

但仍出现TypeScript编译错误,提示找不到UTCString类型,完整错误信息如下:

/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/auth/constants.ts:17:71 - error TS2552: Cannot find name 'UTCString'. Did you mean 'String'?

17 export const SIGN_MESSAGE_TEMPLATE = (address: string, utcTimeString: UTCString) =>
                                                                         ~~~~~~~~~

  /Users/xxx/Documents/GitHub/project-api/node_modules/typescript/lib/lib.es5.d.ts:544:13
    544 declare var String: StringConstructor;
                    ~~~~~~
    'String' is declared here.

    at createTSError (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/xxx/Documents/GitHub/project-api/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19) {
  diagnosticCodes: [ 2552 ]
}

脚本结构如下:

import { TransactionsService } from '../../../src/transactions/transactions.service';
import { AppModule } from '../../../src/app.module';    

const prisma = new PrismaClient();
    
async function main() {
  const app = await NestFactory.createApplicationContext(AppModule);
  const transactionService = app.get(TransactionsService);
 
  //...
 
  await app.close();
}

main()
  .catch(async (e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
解决办法

这个错误的核心是UTCString并非TypeScript原生内置类型,TypeScript无法识别它,可通过以下方式解决:

1. 直接使用原生string类型

如果utcTimeString是UTC格式的字符串,直接替换为string即可:

// src/auth/constants.ts
export const SIGN_MESSAGE_TEMPLATE = (address: string, utcTimeString: string) => {
  // 原函数逻辑
};

2. 自定义类型别名

若想保留UTCString作为语义化标识,在使用它的文件或全局类型文件中定义类型别名:

// src/auth/constants.ts
type UTCString = string;

export const SIGN_MESSAGE_TEMPLATE = (address: string, utcTimeString: UTCString) => {
  // 原函数逻辑
};

3. 检查类型导入

如果UTCString是项目内部或第三方库定义的类型,确认是否在src/auth/constants.ts中正确导入:

// src/auth/constants.ts
import { UTCString } from './path-to-type-definition'; // 替换为实际文件路径

export const SIGN_MESSAGE_TEMPLATE = (address: string, utcTimeString: UTCString) => {
  // 原函数逻辑
};

另外,确保脚本运行时使用的tsconfig.json与项目主配置一致,避免因类型库加载差异引发问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:57:20