1

I'm trying to do some code splitting and moment.js is one of the targeted packages I want to isolate in a separate chunk. I'm doing it like this:

const myFn = async (currentNb) => {
   const moment = (await import('moment')).default()          
   const dateUnix: number = moment(currentNb).valueOf()
   [...]
}

But I get the following error: this expression is not callable. Type Moment has no call signatures . So I guess I should be doing something like moment.valueOf(currentNb) but I'm not really sure. Can someone put some light to this? Thanks in advance

1
  • I'm not sure as to what's the proper type for moment, but it looks like it's a Function.. Worth trying.. Also check out stackoverflow.com/questions/29689966/… Commented Oct 6, 2021 at 13:03

1 Answer 1

1

So apparently my syntax was incorrect. I finally found how to solve this problem. I need to import it as follows:

const {default: moment} = await import('moment')

I found the answer in the webpack documentation:

The reason we need default is that since webpack 4, when importing a CommonJS module, the import will no longer resolve to the value of module.exports, it will instead create an artificial namespace object for the CommonJS module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.