TypeScript 给出错误 Nest.js 不是模块

问题描述 投票:0回答:1

我正在使用 Nest.js 来构建我的项目,但是在工作过程中遇到了一个错误。我没有找到解决这个问题的方法。我正在从文件中导出一个函数并将其导入控制器中,但它给出错误,它不是一个模块,没有进行导出,但我正在导出该函数,如代码中所示:

import { AuthGuard } from '@nestjs/passport';
export class AdminGuard extends AuthGuard ('admin'){
    constructor(){
     
        super();
    }
}

导出此

export * from './admin-jwt.strategy'`

控制器:

import { Controller, Get,UseGuards } from '@nestjs/common';
import { UserService } from './user.service';
import { JwtGuard,AdminGuard } from 'src/auth/guard';
import { DynamicRole } from 'src/auth/decorator';

@UseGuards(AdminGuard,DynamicRole)
@DynamicRole('superadmin', 'support')

@Controller('user')
export class UserController {
    constructor(private userservice:UserService){}
    @Get('me')
    getme(){
        return this.userservice.getme();
    }

}

This error is shown in the console 我尝试重新启动服务器并检查

tsconfig.json
,还检查dist文件夹是否有任何错误,但什么也没发现。

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

我还尝试使用 ECMAScript 6 更改

commonjs
,但弹出其他错误:找不到内置模块,就像您在 this image 中看到的那样。

typescript module nestjs
1个回答
0
投票

问题在于导入

import { JwtGuard,AdminGuard } from 'src/auth/guard';

使用 ../ 代替 src

import { JwtGuard,AdminGuard } from '../auth/guard';
© www.soinside.com 2019 - 2024. All rights reserved.