Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I did following steps

npm install moment --save
import moment from "moment"

When i want to import momentjs i get the following error:

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

moment version: ^2.22.1

I use webpack 4.

Trying to import like this also failed with the same error:

import moment from "moment/src/moment"

Can somebody help me? I really dont know how to solve this. My Webpack Config:

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(),
],
};
like image 688
otto Avatar asked Oct 17 '22 18:10

otto


1 Answers

finally found the solution. Problem is that i imported another npm module where i found this:

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

changing writable false to wirtable true solved the problem

like image 85
otto Avatar answered Oct 21 '22 08:10

otto