首页 > 解决方案 > 导入时强制转换类的名称以防止“不是构造函数”错误

问题描述

导入时可以转换导出类的名称吗?

出口:

class ResponseObj {}
const TYPES = {}
module.exports = { ResponseObj, TYPES };

并导入:

const { response, responseTypes } = require(cfg.libs + 'message_response');

现在我在运行时收到错误“响应不是构造函数”:

var n = new response()

我怎样才能只为一个文件转换它并为其他文件保留 ({ ResponseObj, TYPES })?

标签: node.js

解决方案


const { ResponseObj:response , TYPES:responseTypes } = require(cfg.libs + 'message_response');

就是你要找的。

更普遍,

const { exportedKey: newKeyName } = require('something);

// use newKeyName this file 

推荐阅读