An ES module's interface is not a single value, but a set of named variables.
import ordinal from "ordinal"
import {days, months} from "date-names"
export function formatDate(date, format) {
return format.replace(/YYYY|M(MMM)?|Do|dddd/g, tag => {
if (tag == "YYYY") return date.getFullYear()
if (tag == "M") return date.getMonth()
if (tag == "MMMM") return months[date.getMonth()]
if (tag == "D") return date.getDate()
if (tag == "Do") return ordinal(date.getDate())
if (tag == "dddd") return days[date.getDay()]
})
}
Instead of calling a function to access a dependency you can use the special
import keyword. Similarly, the export keyword is used to export things. It
can appear in front of a variable, function, or class.
When there is a variable named default, it is treated as the module's main
exported value. If you import a module like ordinal in this example, without
braces around the name, you get it's default variable. Such modules can still
export other variables alongside their default export.
export default ["Winter", "Spring", "Summer", "Autumn"]
You can also rename imported variables:
import {days as dayNames} from "date-names"
In nodejs you need to enable ES modules by setting "type": "module" in your package.json.