backpack
backpack copied to clipboard
CommonJS-Modules containing an async function are empty when required
Hi,
I just started to integrate backpack and it seems quite nice, but I encountered the following problem:
When a CommonJS module with an async function is required, the imported object is empty.
Test case:
a.js
exports.x = () => 5 // normal function
b.js
exports.x = async () => 5 // async function
c.js
export const x = async () => 5 // async function
index.js
const a = require('./a')
const b = require('./b')
import {x} from './c'
console.log('a', a) // normal function in CommonJS module ==> works
console.log('b', b) // async function in CommonJS module ==> doesn't work
console.log('c', x) // async function in ES6 module ==> works
Output
a { x: [Function] }
b {}
c function x() {
return _ref.apply(this, arguments);
}
Notice that the object after b
is empty, so the exported functions are not callable. Strange is that the aysnc function works when imported from an ES6 module.
This describes my issue exactly! I didn't realize it was specific to backpack. The post is now 7 months old, there's no additional comment?
Hmmmmm. Must be a babel issue
I solved this issue by using the CommonJS pattern of using module.exports
. Not sure if it's the best solution, however.
exports.x = () => 5 // normal function
const y = async () => 5 // async function
module.exports = { x, y }