justExport.js
const first = () => {
console.log('frist from justExport')
}
const second = () => {
console.log('second fromt justExport')
}
module.exports = {
first,
second,
}tmp.js
module.exports = {
...require('./justExport') // work
require('./justExport') // SyntaxError: Unexpected string
}main.js
const justExport = require('./justExport.js')
const tmp = require('./tmp.js')
console.log('Hello World!')我自愿用不太可能的代码创建了一个假的例子。
发布于 2019-01-06 01:00:12
{ ...require('./justExport') }是object literal spread。而{ require('./justExport') }是不正确的对象文字语法,因为它不包含键。
除非意图是创建justExport模块的浅层副本,否则不需要对象文字。它可以是:
module.exports = require('./justExport');发布于 2019-01-06 01:13:15
为了进一步阐明来自@estus的答案,请注意以下工作是由于ES6 shorthand property names
const justExport = require('./justExport');
module.exports = {
...justExport, // works
justExport // works because key is implicitly defined by variable name
}https://stackoverflow.com/questions/54053775
复制相似问题