【发布时间】:2020-10-30 17:45:40
【问题描述】:
我想在我的 Create React 应用程序中使用 chess.js npm 库,但我不断收到“国际象棋不是构造函数”的错误消息。这是我的代码:
import React from 'react';
import './App.css';
const { Chess } = require('chess.js');
class App extends React.Component
{
constructor(props) {
super(props);
this.state = { board: '' };
let game = new Chess();
this.state.board = game.ascii();
}
render() {
return (
<div className="App">
{/* <InputForm name="Moves: "/> */}
<p>{ this.state.board }</p>
</div>
);
}
}
export default App;
package.json:
{
"name": "blind-chess",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"chess.js": "^0.11.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
有没有人知道我做错了什么?我的代码看起来像文档中提供的示例代码,但它仍然无法正常工作。 https://www.npmjs.com/package/chess.js
【问题讨论】:
-
你是用
npm安装的吗? -
是的,我使用了 npm i 命令
标签: javascript reactjs npm create-react-app