简体   繁体   English

如何将 chess.js 导入 Create React App

[英]How to import chess.js into Create React App

I want to use the chess.js npm library inside my Create React App, but I keep getting the error that "Chess is not a constructor".我想在我的 Create React 应用程序中使用 chess.js npm 库,但我不断收到“国际象棋不是构造函数”的错误。 Here's my code:这是我的代码:

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: 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"
    ]
  }
}

Does any body know what I'm doing wrong?有没有人知道我做错了什么? My code looks like the example code provided in the documentation but it's still not working.我的代码看起来像文档中提供的示例代码,但它仍然无法正常工作。 https://www.npmjs.com/package/chess.js https://www.npmjs.com/package/chess.js

Do you have you're package.json?你有你package.json吗? There is some additional documentation https://github.com/jhlywa/chess.js/blob/master/README.md that may be helpful还有一些额外的文档https://github.com/jhlywa/chess.js/blob/master/README.md可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM