【问题标题】:How to import chess.js into Create React App如何将 chess.js 导入 Create React App
【发布时间】: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


【解决方案1】:

你有你的 package.json 吗?还有一些额外的文档https://github.com/jhlywa/chess.js/blob/master/README.md 可能会有所帮助

【讨论】:

  • 我刚刚发布了我的 package.json,README 与我认为我正确遵循的 npm 网站上的说明相同,但仍然无法正常工作。
猜你喜欢
  • 2019-04-06
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 2018-12-18
  • 1970-01-01
  • 2018-11-27
  • 2018-07-03
相关资源
最近更新 更多