How to Install Dayjs: npm, CDN and Direct Download

Every route for adding Dayjs to a project, from a single npm command to a CDN script tag, with checks that confirm everything is wired up correctly.

Updated 15 November 20267 min readGetting started
Laptop on a desk showing code while Dayjs is installed

Adding a date library ought to take minutes, not an afternoon. Dayjs was designed with that in mind: it has no runtime dependencies, ships its own TypeScript definitions and works the same way in browsers and in Node.js. This guide walks through every practical route for installing Dayjs, explains when each one makes sense and finishes with a short checklist that confirms your setup is sound before you write a single line of business logic.

Whichever method you choose, you end up with the same library. That matters, because it means a prototype that starts life with a CDN script tag can later move into a bundled application without any change to the code that formats or manipulates dates.

Before you begin: what you need

For package-manager installs you need a recent version of Node.js, which brings npm with it. Any currently supported Node.js release is suitable. If your team prefers Yarn or pnpm, those work equally well. For browser-only use through a CDN, you need nothing more than an HTML file and a text editor.

It is also worth deciding early whether your project uses ES modules or CommonJS. Modern front-end tooling such as Vite, webpack and Rollup favours ES modules, while older Node.js scripts often use require. Dayjs supports both, but knowing which style you use avoids confusion with import statements later.

Installing Dayjs with npm, Yarn or pnpm

For the overwhelming majority of projects, installing from the npm registry is the right choice. It records the dependency in package.json, lets you update with a single command and allows your bundler to include only the code you actually use.

npm install dayjs
yarn add dayjs
pnpm add dayjs

Run whichever command matches your package manager from the root of your project. The package is placed in node_modules and the version is written to your lock file, which ensures every member of the team and every build server receives exactly the same release. At the time of writing the current stable version is 1.11.23.

If you need to pin a specific release, append the version number, for example npm install dayjs@1.11.23. Pinning is sensible for projects with strict change control, although the 1.x line has a long record of careful, backwards-compatible updates.

Importing Dayjs into your code

Once installed, the library is imported like any other module. In an ES module environment the default export is the dayjs function itself.

import dayjs from 'dayjs'

console.log(dayjs().format('dddd, D MMMM YYYY'))

In CommonJS the equivalent uses require. Both styles return the same function with the same methods.

const dayjs = require('dayjs')

console.log(dayjs().format('YYYY-MM-DD'))

A common question is whether to import the library once in a central file or in every module that needs it. Either works. Because modules are cached, importing Dayjs in ten files does not load it ten times. What does matter is where you load plugins and locales, which we cover below.

Using Dayjs from a CDN

For static pages, quick experiments, coding exercises or content management systems where a build step is impractical, a CDN offers the fastest route. A single script tag makes a global dayjs function available to every script that follows it.

<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script>
  document.querySelector('#today').textContent = dayjs().format('D MMM YYYY')
</script>

Popular public CDNs such as jsDelivr, unpkg and cdnjs all mirror the npm package. Using dayjs@1 in the URL always serves the newest 1.x release, while a full version such as dayjs@1.11.23 locks the file. For production pages, a locked version combined with a Subresource Integrity hash is the more cautious option, since it guarantees the file has not changed.

Plugins and locales are available from the same CDN. Each plugin attaches itself to a global variable named after the plugin, prefixed with dayjs_plugin_, which you then pass to dayjs.extend().

<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/relativeTime.js"></script>
<script>
  dayjs.extend(window.dayjs_plugin_relativeTime)
  console.log(dayjs('2026-01-01').fromNow())
</script>

Downloading the Dayjs source archive

Some environments cannot reach the public registry: air-gapped networks, regulated industries or teams that audit every third-party file before it enters a repository. In those cases, downloading the full source archive is the practical answer. The archive contains the core library, every plugin, all 143 locales, the TypeScript declarations and the test suite.

After extracting the archive you can copy the prebuilt files you need into your project, or run the build yourself to produce a customised bundle. You can find the archive in the download section of our homepage. Keep in mind that a manually copied file will not update itself, so record the version you downloaded and review it periodically.

Setting up Dayjs with TypeScript

Dayjs includes its own type definitions, so there is no separate types package to install. The only adjustment many projects need is a compiler option that allows default imports from CommonJS-style modules.

// tsconfig.json
{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}

With those options enabled, import dayjs from 'dayjs' works as expected and your editor offers autocompletion for every method. Plugins also ship with type declarations that augment the main Dayjs interface, so a method such as fromNow() becomes visible to the compiler as soon as you import and extend the relevant plugin.

Loading plugins and locales in one place

Plugins and locales register themselves globally, which means they need to be loaded once before any code relies on them. The tidiest approach is a small setup module that every other part of the application imports.

// src/lib/date.js
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/en-gb'

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(relativeTime)
dayjs.locale('en-gb')

export default dayjs

Components then import from this file rather than from the package directly. The benefit is consistency: every part of the codebase sees the same plugins and the same default locale, and adding a new plugin later is a one-line change. Our guide to Dayjs plugins explains which plugins are worth loading by default.

Framework notes: React, Vue, Angular and Next.js

Dayjs is framework-agnostic, so installation is identical whether you build with React, Vue, Svelte or Angular. The setup module described above slots neatly into any of them. In React, import the configured instance into components and format values during rendering. In Vue, you may choose to expose it through a composable or a global property, although a plain import is often clearer.

Server-rendered frameworks such as Next.js and Nuxt deserve one extra thought. The server and the browser may sit in different time zones, which can produce mismatched output during hydration. The usual remedy is to format dates in UTC on the server, or to defer time-zone-specific formatting until the component has mounted on the client. The time zones and UTC guide describes both strategies.

Verifying your Dayjs installation

A few quick checks confirm that everything is wired up correctly:

  • Basic output. Log dayjs().format(). You should see an ISO-style string with today's date and your offset.
  • Version. Log dayjs.version or run npm ls dayjs to confirm which release is installed.
  • Plugins. If you extended a plugin, call one of its methods, such as dayjs().fromNow(). A “not a function” error means the plugin was not extended before use.
  • Locale. Format a month name. If it appears in English when you expected another language, the locale file was not imported.

Troubleshooting common installation problems

Most installation issues fall into a handful of familiar patterns. An error stating that dayjs is not a function in TypeScript usually means esModuleInterop is disabled. Duplicate copies of the library, which can happen when two dependencies require different versions, may cause plugins to appear missing; running npm dedupe or aligning versions resolves it. In browser projects, a plugin that seems absent is often loaded before the core script, so check the order of your script tags.

Finally, remember that a CDN request can be blocked by strict content security policies. If the global dayjs is undefined on a production site but works locally, review your policy headers and allow the CDN host explicitly, or serve the file from your own domain.

Keeping Dayjs up to date

Installing a library is a one-off task, but maintaining it is ongoing. With a package manager, run npm outdated from time to time to see whether a newer release is available, then read the changelog before upgrading. The 1.x series has a long record of backwards-compatible releases, so updates are usually painless, yet a quick review of the notes remains good practice, particularly for projects that depend heavily on parsing or time zones.

Automated tools such as Dependabot or Renovate can open update requests for you, complete with release notes. Pair them with a test suite that formats and manipulates a handful of fixed dates, and you will know within minutes whether an upgrade is safe to merge. For CDN users, a pinned version number combined with a periodic manual review offers the same control without a build pipeline.

Finally, keep an eye on the size of what you ship. A bundle analyser shows exactly how much space Dayjs, its plugins and its locales occupy in your production build. If a plugin or locale is no longer used, remove the import from your setup module and enjoy the savings immediately.

Conclusion

Installing Dayjs is refreshingly simple. For bundled applications, a single npm, Yarn or pnpm command followed by a default import is all it takes. For static pages, a CDN script tag works immediately, and for restricted environments the downloadable archive gives you every file in one place. Whichever route you take, gather plugins and locales into one setup module, confirm the basics with a quick console check and you will have a dependable foundation. Once you are up and running, the natural next step is learning to present dates clearly, which our formatting guide covers in detail.