将 Bun 与 @nestjs/config 一起使用时,我在将配置注入模块时遇到问题。
有什么方法可以将@nestjs/config与Bun一起使用吗?
import { ConfigType } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { forwardRef, Inject, Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {
/**
* Construct for inject services and repositories
* @param usersRepository
*/
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
@Inject(profileConfig.KEY)
private readonly profileConfiguration: ConfigType<typeof profileConfig>,
) {}
}
我尝试使用 npm/Nodejs 运行,一切正常,但不适用于 Bun。
这是与 Bun 一起使用时的输出:
1 | (function (entry, fetcher)
^
SyntaxError: Export named 'ConfigType' not found in module 'path_to_project/node_modules/@nestjs/config/index.js'.
1 | (function (entry, fetcher)
^
SyntaxError: Export named 'ConfigType' not found in module 'path_to_project/node_modules/@nestjs/config/index.js'.
要解决此问题,请尝试直接从
ConfigType
导入 '@nestjs/config'
,而不指定 /index.js
部分。当包含 /index.js
时,Bun 可能无法正确解释模块路径。
将导入语句替换为以下内容:
import { ConfigType } from '@nestjs/config';
所以你的代码变成:
import { ConfigType } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { forwardRef, Inject, Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {
/**
* Construct for inject services and repositories
* @param usersRepository
*/
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
@Inject(profileConfig.KEY)
private readonly profileConfiguration: ConfigType<typeof profileConfig>,
) {}
}