You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ES6模块与Webpack导入Moment.js时遇只读属性赋值错误求助

Webpack 4 + Moment.js ^2.22.1: 导入时出现Uncaught TypeError错误

各位大佬好,我最近在项目里尝试使用Moment.js,但是导入的时候遇到了一个棘手的错误,折腾了好久都没搞定,想请教下大家的解决方案。

我先是执行了安装命令:

npm install moment --save

然后在代码里这样导入:

import moment from "moment"

结果运行时直接抛出了这个错误:

Uncaught TypeError: Cannot assign to read only property 'clone' of object '#<Moment>' (moment.js:3837 )

我的Moment.js版本是^2.22.1,项目用的是Webpack 4。我还试过直接导入源码文件:

import moment from "moment/src/moment"

结果还是出现一模一样的错误。

下面是我的Webpack配置文件,麻烦大家帮忙看看哪里可能出问题了:

const path = require('path')
const BrowserSyncPlugin = require("browser-sync-webpack-plugin")
var webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
 entry: './src/js/index.js',
 output: {
 path: path.resolve(__dirname, 'static'),
 filename: 'monitor-bundle.js'
 },
 devtool: 'source-map',
 mode: 'development',
 module: {
 rules: [
 { test: /\.js$/,
 exclude: /node_modules/,
 use: { loader: "babel-loader" }
 },
 { test: /\.css$/,
 }
 ]
 },
 watch: true,
 plugins: [
 new BrowserSyncPlugin({
 watchOptions: { poll: true },
 host: "localhost",
 port: "1337",
 proxy: "http://localhost:80/",
 files: ["./static/monitor-bundle.js"],
 open: true,
 reloadDelay: 0,
 reloadDebounce: 0,
 browser: ["chromium", "google chrome"]
 }),
 new BundleAnalyzerPlugin(),
 ],
};

内容的提问来源于stack exchange,提问作者otto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:18:12