javascript - 使用 es6 模块和 webpack 导入 momentjs 无法分配给只读属性
问题描述
我做了以下步骤
npm install moment --save
import moment from "moment"
当我想导入 momentjs 时,出现以下错误:
Uncaught TypeError: Cannot assign to read only property 'clone' of object '#<Moment>' (moment.js:3837 )
瞬间版本:^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(),
],
};
解决方案
终于找到了解决办法。问题是我导入了另一个 npm 模块,在那里我发现了这个:
Object.defineProperty(Array.prototype, "clone", {
value: function(){
return this.slice(0)
},
enumerable: false,
writable: false,
})
将 writable false 更改为 wirtable true 解决了问题
推荐阅读
- python - Django Rest 框架错误'django.core.urlsolvers'
- ansible - ansible add_host 从文件中设置变量
- python - 在 pandas 中查找组中第二个最早的时间
- apache-spark - Airflow + Kubernetes VS Airflow + Spark
- excel - 如何在不从剪贴板粘贴内容的情况下在 excel VBA 中插入列
- r - 如何在R中一次清理多个excel文件?
- javascript - Uncaught ReferenceError: obj 未定义 onclick
- javascript - 如何在 HTML 中有条件的换行
- python - 在 Django 中管理继承
- vba - 错误 1004 vba 数据透视过滤器包含值