使用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
相关产品推荐
相关产品推荐

