Uncaught TypeError:无法分配为仅读取对象“”的属性“ exports”

时间:2018-10-15 17:45:50

标签: javascript reactjs

我知道在SO和其他论坛中有很多与此标题相关的问题,但是没有一个问题解决了我的问题(我不确定这样做是否正确)。我想使用canvas.js绘制画布。另外,我正在使用React和它的新功能。我的React项目的结构如下:

+node_modules
+public
+src
  -canvas.min.js
  -canvas.react.js
  -App.js
  -index.js
  -indes.css
-package.json

我的App.js文件包含以下代码:

    /* App.js */
import React from 'react';
import CanvasJS from './canvasjs.min';
var CanvasJSReact = require('./canvasjs.react');
var Component = React.Component;
var CanvasJS = CanvasJSReact.CanvasJS;
var CanvasJSChart = CanvasJSReact.CanvasJSChart;


class App extends Component {   
  render() {
    const options = {
      title: {
        text: "Basic Column Chart in React"
      },
      data: [{              
                type: "column",
                dataPoints: [
                    { label: "Apple",  y: 10  },
                    { label: "Orange", y: 15  },
                    { label: "Banana", y: 25  },
                    { label: "Mango",  y: 30  },
                    { label: "Grape",  y: 28  }
                ]
       }]
   }

   return (
      <div>
        <CanvasJSChart options = {options}
            /* onRef = {ref => this.chart = ref} */
        />
      </div>
    );
  }
}
//module.exports = App;                              
export default App;

index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(<App/>, document.getElementById('app'));

当我在cmd中运行npm start时,会发生此错误:

Uncaught TypeError: Cannot assign to read only property ‘exports’ of object ‘

1 个答案:

答案 0 :(得分:0)

如果您的canvas.js是ES6模块,则应使用

import CanvasJS from './canvasjs.min';

如果它是一个简单的commonjs或umd模块,请使用

var CanvasJS = require('./canvasjs.min');