← Back to moment/moment

How to Deploy & Use moment/moment

# Moment.js Deployment & Usage Guide

## 1. Prerequisites

- **Node.js** (>= 8.0.0 recommended, though the library maintains compatibility with older environments)
- **npm** (comes with Node.js)
- **Git**
- **Optional**: [Sauce Labs](https://saucelabs.com/) account for cross-browser testing (requires `.sauce-labs.creds` file)

## 2. Installation

Clone the repository and install dependencies:

```bash
git clone https://github.com/moment/moment.git
cd moment
npm install

This installs Grunt, Rollup, Karma, and all build/test dependencies.

3. Configuration

Sauce Labs (Optional)

For cross-browser testing via Sauce Labs, create a .sauce-labs.creds file in the project root:

{
  "username": "your-sauce-username",
  "accessKey": "your-sauce-access-key"
}

Note: If this file is missing, Sauce Labs tests will be skipped.

Environment Variables

No additional environment variables are required for basic usage. The build system uses standard Node.js environment detection.

4. Build & Run

Development Workflow

Run tests locally:

npm test
# or
grunt test

Build distribution files:

grunt build

This generates:

  • min/moment.js - Core library (minified)
  • min/moment-with-locales.js - Core + all locales
  • min/tests.js - Test suite bundle

Run specific test suites:

# Unit tests only
grunt karma:unit

# Full cross-browser tests (requires Sauce Labs)
grunt karma:sauce

Build System Details

The project uses Grunt with Rollup for transpilation:

  • Entry point: src/moment.js
  • Format: UMD (Universal Module Definition) for browser/Node.js compatibility
  • Output: build/tmp/ (intermediate) → min/ (final)

Key Grunt tasks:

  • grunt transpile - Bundles ES6 modules into distributable UMD format
  • grunt karma - Runs QUnit tests via Karma test runner
  • grunt build - Full build pipeline (transpile + minify)

5. Deployment

Publishing to NPM

Since Moment.js is a library (not an application), deployment means publishing to the npm registry:

# 1. Run full test suite
npm test

# 2. Build distribution files
grunt build

# 3. Verify package contents
npm pack

# 4. Publish (requires npm permissions)
npm publish

Important: This project is in maintenance mode. See Project Status before publishing new versions.

Browser Usage

Include built files directly:

<!-- Core only -->
<script src="min/moment.min.js"></script>

<!-- With all locales -->
<script src="min/moment-with-locales.min.js"></script>

<!-- Specific locale -->
<script src="min/moment.min.js"></script>
<script src="locale/ru.js"></script>

CDN Deployment

For production use via CDN (no build required):

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Creating a Release

  1. Update version in package.json following SemVer
  2. Update CHANGELOG.md
  3. Build: grunt build
  4. Commit: git commit -am "Release version X.Y.Z"
  5. Tag: git tag X.Y.Z
  6. Push: git push && git push --tags
  7. Publish: npm publish

6. Troubleshooting

Build Issues

Error: Cannot find module 'rollup'

  • Ensure you ran npm install in the project root
  • Check Node.js version compatibility (>= 6.0 required for Rollup)

Error: ENOENT: no such file or directory, open 'build/tmp/moment'

  • Run grunt transpile before grunt build
  • The build directory is created during the transpilation step

Test Failures

Sauce Labs authentication errors

  • Verify .sauce-labs.creds exists and contains valid JSON
  • Check that credentials are not expired
  • Run without Sauce Labs: grunt karma:unit

Locale tests failing

  • Some locale tests require specific system locales to be installed
  • Run with grunt karma:unit to skip Sauce Labs locale-specific edge cases

Module Loading Issues

"moment is not defined" in browser

  • Ensure you're loading the UMD build (min/moment.js) not the ES6 source
  • For ES6 module usage: import moment from 'moment' (requires bundler)

Locale not loading

  • When using individual locales, load the core library first: <script src="moment.js"></script> then <script src="locale/ru.js"></script>
  • Or use moment-with-locales.js which includes all locales

Legacy Maintenance Mode Issues

  • Security vulnerabilities: Moment.js is in maintenance mode. Consider migrating to Luxon, Day.js, or native Intl.DateTimeFormat for new projects.
  • Bundle size: If build output is too large, use moment/min/moment.min.js instead of moment-with-locales, and import only required locales.