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

前端之家收集整理的这篇文章主要介绍了javascript – 使用es6模块和webpack导入momentjs无法分配给只读属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我做了以下步骤

  1. npm install moment --save
  2. import moment from "moment"

当我想导入momentjs时,我收到以下错误

  1. Uncaught TypeError: Cannot assign to read only property 'clone' of object '#

时刻版:^ 2.22.1

我使用webpack 4.

尝试像这样导入也失败了同样的错误

  1. import moment from "moment/src/moment"

有人能帮助我吗?我真的不知道如何解决这个问题.
我的Webpack配置:

  1. const path = require('path')
  2. const BrowserSyncPlugin = require("browser-sync-webpack-plugin")
  3. var webpack = require('webpack');
  4. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  5. module.exports = {
  6. entry: './src/js/index.js',output: {
  7. path: path.resolve(__dirname,'static'),filename: 'monitor-bundle.js'
  8. },devtool: 'source-map',mode: 'development',module: {
  9. rules: [
  10. {
  11. test: /\.js$/,exclude: /node_modules/,use: {
  12. loader: "babel-loader"
  13. }
  14. },{
  15. test: /\.css$/,}
  16. ]
  17. },watch: true,plugins: [
  18. new BrowserSyncPlugin({
  19. watchOptions: {
  20. poll: true
  21. },host: "localhost",port: "1337",proxy: "http://localhost:80/",files: ["./static/monitor-bundle.js"],open: true,reloadDelay: 0,reloadDebounce: 0,browser: ["chromium","google chrome"]
  22. }),new BundleAnalyzerPlugin(),],};
最佳答案
终于找到了解决方案.问题是我导入了另一个npm模块,我发现了这个:

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

将可写错误改为wirtable true解决了这个问题

猜你在找的JavaScript相关文章