簡體   English   中英

如何將 chess.js 導入 Create React App

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

我想在我的 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

你有你package.json嗎? 還有一些額外的文檔https://github.com/jhlywa/chess.js/blob/master/README.md可能會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM