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

时间:2018-05-11 13:30:18

标签: javascript webpack ecmascript-6 momentjs webpack-4

我做了以下步骤

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(),
],
};

1 个答案:

答案 0 :(得分:3)

终于找到了解决方案。问题是我导入了另一个npm模块,我发现了这个:

Object.defineProperty(Array.prototype, "clone", {
    value: function(){
        return this.slice(0)
    },
    enumerable: false,
    writable: false,
})

将writable false更改为wirtable true解决了问题