Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use dayjs in Vite for Vue or React?

Tags:

vite

dayjs

If you attempt to import dayjs into a Vue/React app using Vite you will find it fails. Vite only works with ESM modules.

like image 690
mbokil Avatar asked Sep 06 '25 03:09

mbokil


2 Answers

I found you need to import the esm module directly and then it will work correctly with Vite run/build.

import dayjs from 'dayjs/esm/index.js'
like image 73
mbokil Avatar answered Sep 07 '25 21:09

mbokil


DayJS Docs are confusing. Vite dev working OK, BUT facing errors on vite build/preview. Problem was in my code:

WRONG:

import dayjs from "dayjs";
import * as customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);

CORRECT:

import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);
like image 21
lcsd Avatar answered Sep 07 '25 21:09

lcsd