{"id":15467,"library":"moment-timezone-data-webpack-plugin","title":"Moment Timezone Data Webpack Plugin","description":"This webpack plugin, `moment-timezone-data-webpack-plugin`, is designed to significantly reduce the bundle size of `moment-timezone` by selectively stripping unneeded time zone data during the webpack build process. As of version 1.5.1, it provides critical functionality for applications that use `moment-timezone` but don't require its entire historical and global dataset, which can be over 900KiB raw. The plugin actively processes the `moment-timezone` data module, allowing developers to specify date ranges (e.g., `startYear`, `endYear`), specific zones via regular expressions (`matchZones`), or countries (`matchCountries`). This addresses a common issue where `moment-timezone` includes all data by default in webpack builds, even if runtime configuration limits the data usage. The plugin is in active maintenance, with recent updates ensuring compatibility with NodeJS 18 and Webpack 5. It works complementarily with `moment-locales-webpack-plugin` for further bundle size optimizations.","status":"active","version":"1.5.1","language":"javascript","source_language":"en","source_url":"https://github.com/gilmoreorless/moment-timezone-data-webpack-plugin","tags":["javascript","webpack","moment","timezone","tzdata","typescript"],"install":[{"cmd":"npm install moment-timezone-data-webpack-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add moment-timezone-data-webpack-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add moment-timezone-data-webpack-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the core library whose data is processed and optimized by this webpack plugin.","package":"moment-timezone","optional":false},{"reason":"Serves as a webpack plugin, tightly integrated with webpack's build process and configuration. Supports webpack 4.x.x and 5.x.x.","package":"webpack","optional":false}],"imports":[{"note":"For `webpack.config.js` using CommonJS, the plugin is a default export requiring direct assignment.","wrong":"import MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';","symbol":"default","correct":"const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');"},{"note":"For `webpack.config.mjs` or `webpack.config.ts` (with appropriate `module` resolution), the plugin is the default export.","wrong":"const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');","symbol":"default","correct":"import MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';"},{"note":"Regardless of import style, the plugin must be instantiated with the `new` keyword within the `plugins` array of your webpack configuration.","wrong":"plugins: [MomentTimezoneDataPlugin({ /* options */ })]","symbol":"MomentTimezoneDataPlugin","correct":"plugins: [new MomentTimezoneDataPlugin({ /* options */ })]"}],"quickstart":{"code":"import path from 'path';\nimport { Configuration } from 'webpack';\nimport MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';\n\nconst config: Configuration = {\n  mode: 'production',\n  entry: './src/index.ts', // Assuming a simple entry point\n  output: {\n    path: path.resolve(__dirname, 'dist'),\n    filename: 'bundle.js',\n  },\n  module: {\n    rules: [\n      {\n        test: /\\.ts$/,\n        use: 'ts-loader',\n        exclude: /node_modules/,\n      },\n    ],\n  },\n  resolve: {\n    extensions: ['.ts', '.js'],\n  },\n  plugins: [\n    // This example limits timezone data to specific years and only for zones matching 'Europe/'\n    // and also includes all zones for 'AU' (Australia).\n    // It's crucial to ensure these settings cover all needed timezones for your application.\n    new MomentTimezoneDataPlugin({\n      startYear: 2020,\n      endYear: 2030,\n      matchZones: /Europe\\//, // Only include zones matching 'Europe/'\n      matchCountries: ['AU'], // Include all zones for Australia\n      cacheDir: path.resolve(__dirname, '.moment-timezone-cache') // Optional: custom cache directory\n    }),\n    // Optionally, if also stripping locales\n    // new MomentLocalesWebpackPlugin({\n    //   localesToKeep: ['en', 'es'],\n    // }),\n  ],\n};\n\nexport default config;\n","lang":"typescript","description":"This Webpack configuration demonstrates how to integrate `moment-timezone-data-webpack-plugin` to filter timezone data. It sets a date range (2020-2030), includes zones starting with 'Europe/', and all zones for Australia, significantly reducing the bundled `moment-timezone` data size."},"warnings":[{"fix":"Carefully define `startYear`, `endYear`, `matchZones`, and `matchCountries` options. Consider a wider range or more zones if unsure, and use a staging environment for testing.","message":"Over-aggressive data stripping can lead to runtime errors where `moment-timezone` reports missing data for dates or zones your application unexpectedly needs. Always thoroughly test your application after configuring this plugin.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade your build environment to a supported Node.js version (e.g., Node.js 16 or newer) to ensure compatibility and receive official support.","message":"As of v1.5.1, the plugin no longer officially tests against NodeJS 8 or 10. While no explicit code changes were made to break compatibility, usage on these End-of-Life Node.js versions is not guaranteed and unsupported.","severity":"breaking","affected_versions":">=1.5.1"},{"fix":"Ensure `moment-timezone` (`>=0.1.0`) and `webpack` (`4.x.x` or `5.x.x`) are installed as peer dependencies or compatible versions in your project.","message":"Incorrectly installing `moment-timezone` or `webpack` as non-peer dependencies, or incompatible versions, can cause build failures. The plugin relies on specific `webpack` APIs and `moment-timezone`'s internal structure.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always instantiate the plugin with `new MomentTimezoneDataPlugin({...})` in your webpack configuration file.","message":"The plugin requires `new` keyword when instantiating in `webpack.config.js`. Forgetting it will result in an error indicating the plugin is not a constructor.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change `plugins: [MomentTimezoneDataPlugin({...})]` to `plugins: [new MomentTimezoneDataPlugin({...})]`.","cause":"The `new` keyword was omitted when instantiating the plugin in the webpack configuration.","error":"TypeError: MomentTimezoneDataPlugin is not a constructor"},{"fix":"Install `moment-timezone` using `npm install moment-timezone` or `yarn add moment-timezone`.","cause":"The `moment-timezone` package is not installed or not resolvable by webpack.","error":"Module not found: Error: Can't resolve 'moment-timezone' in..."},{"fix":"Adjust the plugin options (`startYear`, `endYear`, `matchZones`, `matchCountries`) to include the necessary timezone data. Thoroughly test all time-related functionalities.","cause":"Your application is attempting to use a timezone or date range that was stripped by the plugin configuration.","error":"ERROR in [MomentTimezoneDataPlugin] Error: The timezone data for '...' is not available in the specified range..."},{"fix":"Verify that your `webpack` version (`4.x.x` or `5.x.x`) is compatible with the plugin version. Ensure `webpack` and `moment-timezone-data-webpack-plugin` are correctly installed and configured in your `package.json` and `webpack.config.js`.","cause":"Often indicates an incompatibility between the plugin and the installed `webpack` version, or an issue with the webpack configuration itself where the plugin is not correctly initialized.","error":"Webpack compilation error: Cannot read properties of undefined (reading 'compilation')"}],"ecosystem":"npm"}