backpack icon indicating copy to clipboard operation
backpack copied to clipboard

CommonJS-Modules containing an async function are empty when required

Open barbogast opened this issue 7 years ago • 3 comments

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.

barbogast avatar Jun 30 '17 08:06 barbogast

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?

csmikle avatar Jan 18 '18 22:01 csmikle

Hmmmmm. Must be a babel issue

jaredpalmer avatar Jan 18 '18 22:01 jaredpalmer

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 }

thiagopsnfg avatar Mar 07 '18 12:03 thiagopsnfg