我有一个自定义错误类:
export class CustomAPIError extends Error{
constructor(message, statusCode){
super(message)
this.statusCode = statusCode
}
}
export const createCustomError = (message,statusCode) => {
return new CustomAPIError(message,statusCode)
}
我已将其导入到错误中间件中,如下所示
import {customAPIError} from '../errors/custom.js'
但它给出了一个错误说
import {customAPIError} from '../errors/custom.js'
^^^^^^^^^^^^^^
SyntaxError: The requested module '../errors/custom.js' does not provide an export named 'customAPIError'
如何修复此错误?
我尝试在自定义错误类末尾使用一行来导出它,如下所示
export {createCustomError, CustomAPIError}
但它仍然给出同样的错误。另外,我已经在包 json 文件中添加了
type: module
试试这个:
import { CustomAPIError } from '../errors/custom.js'