使用 es6 模块和 webpack 导入 momentjs 无法分配为只读 属性

import momentjs with es6 modules and webpack Cannot assign to read only property

我做了以下步骤

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 解决了问题