Fix Module can only be default-imported using the ‘allowSyntheticDefaultImports’ flag issue

Today, I want to add moment library to my angular project to format the date, but when I import the moment on the component file, it report…

Today, I want to add moment library to my angular project to format the date, but when I import the moment on the component file, it report the following error:

Module ‘“…/node_modules/moment/ts3.1-typings/moment”’ can only be default-imported using the ‘allowSyntheticDefaultImports’ flag

And I try to fix this error, the first solution is add allowSyntheticDefaultImports flag on tsconfig.js file, the project compiled with no error, but the error still there on the IDE Visual Studio Code. And finally find the solution from the link: This module is declared with using ‘export =’, and can only be used with a default import when using the ‘esModuleInterop’ flag, the solution is as following:

import * as blah from ‘blah-lib’;

For moment library, the import statement is like below:

import * as moment from ‘moment’;

I remember I encounter this issue before, but I can’t find the solution anymore, so I need to search from Google again. So I want to keep it here and hope next time, I can find it here.

Format date with moment library

moment(date * 1000).format(‘YYYY-MM-DD HH:mm:ss’)
# The result will like: 2022–02–02 16:05:36