我有一个index.js
文件,该文件具有以下导出功能:
const mainPipe = require('./mainPipe')
const mongooseDriverPipe = require('./mongooseDriverPipe')
const neo4jDriverPipe = require('./neo4jDriverPipe')
const postgreDriverPipe = require('./postgreDriverPipe')
const redisDriverPipe = require('./redisDriverPipe')
exports.mainPipe = mainPipe
exports.mongooseDriverPipe = mongooseDriverPipe
exports.neo4jDriverPipe = neo4jDriverPipe
exports.postgreDriverPipe = postgreDriverPipe
exports.redisDriverPipe = redisDriverPipe
然后我导入:
const { mongooseDriverPipe } = require('../../pipes')
console.log(mongooseDriverPipe) // undefined
如果我:
const pipes = require('../../pipes')
console.log(pipes) // Logs an object with the above five keys
console.log(pipes.mongooseDriverPipe) // Logs the expected result
这还会返回undefined
:
const test = require('../../pipes').mongooseDriverPipe
console.log(test) // undefined
我所有的路径都是正确的,当我不进行结构分解时,可以看到导出的对象及其所有键,但是当我尝试进行结构分解时,所有经结构化的键始终为undefined
。
我不明白为什么键会被破坏为undefined
。
答案 0 :(得分:-1)
尝试将此行添加到index.js文件的顶部;
module.exports = exports = {};
Require返回module.exports值。
您可以在此处查看更多信息,https://www.freecodecamp.org/news/node-js-module-exports-vs-exports-ec7e254d63ac/
如果愿意,也可以这样定义它们
module.exports = exports = {
mainPipe : require('./mainPipe'),
mongooseDriverPipe : require('./mongooseDriverPipe'),
neo4jDriverPipe : require('./neo4jDriverPipe'),
postgreDriverPipe : require('./postgreDriverPipe'),
redisDriverPipe : require('./redisDriverPipe'),
}