最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Uncaught TypeError: LRU is not a constructor" when trying to connect to a MySQL server via Seque

programmeradmin1浏览0评论

I'm trying to make a web application based on boardgame.io that is just their Tic-Tac-Toe tutorial game, but the results of each match get saved to a MySQL database. In its current state, my code just tries to connect to the database, the result of which should be displayed into the browser's console.

import { Client } from 'boardgame.io/client';
import { TicTacToe } from './Game';

class TicTacToeClient {
  constructor(rootElement) {
    this.client = Client({ game: TicTacToe });
    this.client.start();
    this.rootElement = rootElement;
    this.createBoard();
    this.attachListeners();
    this.client.subscribe(state => this.update(state));
    const { Sequelize, DataTypes } = require("sequelize");
    const sequelize = new Sequelize(
      'tictactoetest',
      'xxxx',
      'xxxx',
       {
         host: 'localhost',
         dialect: 'mysql',
         dialectModule: require('mysql2')
       }
     );
     
     sequelize.authenticate().then(() => 
     {
         console.log('Connection has been established successfully.');
      }).catch((error) => {
         console.error('Unable to connect to the database: ', error);
      });
      const Record = sequelize.define("record", 
      {
          log: 
          {
            type: DataTypes.STRING,
            allowNull: false
          },
          winner: 
          {
            type: DataTypes.STRING,
            allowNull: false
          }
      }, {
          tableName: 'record'
      });
  
      sequelize.sync().then(() => {
          console.log('Record table created successfully!');
       }).catch((error) => {
          console.error('Unable to create table : ', error);
       });
  }

  createBoard() 
  {
    //Irrelevant
  }

  attachListeners() 
  {
    //Irrelevant
  }

  update(state) 
  {
    //Irrelevant
  }
}

const appElement = document.getElementById('app');
const app = new TicTacToeClient(appElement);

The game itself works properly, but instead of the confirmation of success/failure, I get "Uncaught TypeError: LRU is not a constructor". I have tried installing all the LRU libraries I could with NPM, nothing helps. I have successfully ran the same DB connection code in a separate file using "node", so I have no idea where the issue could be.

I'm trying to make a web application based on boardgame.io that is just their Tic-Tac-Toe tutorial game, but the results of each match get saved to a MySQL database. In its current state, my code just tries to connect to the database, the result of which should be displayed into the browser's console.

import { Client } from 'boardgame.io/client';
import { TicTacToe } from './Game';

class TicTacToeClient {
  constructor(rootElement) {
    this.client = Client({ game: TicTacToe });
    this.client.start();
    this.rootElement = rootElement;
    this.createBoard();
    this.attachListeners();
    this.client.subscribe(state => this.update(state));
    const { Sequelize, DataTypes } = require("sequelize");
    const sequelize = new Sequelize(
      'tictactoetest',
      'xxxx',
      'xxxx',
       {
         host: 'localhost',
         dialect: 'mysql',
         dialectModule: require('mysql2')
       }
     );
     
     sequelize.authenticate().then(() => 
     {
         console.log('Connection has been established successfully.');
      }).catch((error) => {
         console.error('Unable to connect to the database: ', error);
      });
      const Record = sequelize.define("record", 
      {
          log: 
          {
            type: DataTypes.STRING,
            allowNull: false
          },
          winner: 
          {
            type: DataTypes.STRING,
            allowNull: false
          }
      }, {
          tableName: 'record'
      });
  
      sequelize.sync().then(() => {
          console.log('Record table created successfully!');
       }).catch((error) => {
          console.error('Unable to create table : ', error);
       });
  }

  createBoard() 
  {
    //Irrelevant
  }

  attachListeners() 
  {
    //Irrelevant
  }

  update(state) 
  {
    //Irrelevant
  }
}

const appElement = document.getElementById('app');
const app = new TicTacToeClient(appElement);

The game itself works properly, but instead of the confirmation of success/failure, I get "Uncaught TypeError: LRU is not a constructor". I have tried installing all the LRU libraries I could with NPM, nothing helps. I have successfully ran the same DB connection code in a separate file using "node", so I have no idea where the issue could be.

Share Improve this question edited Apr 17, 2023 at 19:07 Albina 1,9853 gold badges9 silver badges20 bronze badges asked Apr 17, 2023 at 17:00 Samuel BucherSamuel Bucher 811 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

If you npm ls lru-cache, I'm willing to bet that one of your direct or transitive dependencies depend on version 8 or earlier of lru-cache, and are instead getting version 9 or 10.

I'm also willing to bet, if this is the case, that you're using yarn. It's broken. I remend switching to npm or pnpm, since these are capable of resolving a package tree according to the stated dependency ranges.

Alright, I've figured this out "myself". I needed to mess with the versions of in package.json

"dependencies": {
"mysql2": "^2.2.5",
"pg-hstore": "^2.3.4",
"sequelize": "^5.22.5"
},

Separating the client and the server also helps.

Sometimes there may be lots of different versions in npm list lru-cache.

If you want to ensure LRU works as expected, you can use use-m library. It will guarantee that you will get specific version of lru-cache, that works as constructor. That is useful to isolate your specific code example from other code, to investigate the reason of the problem faster.

For CommonJS:

const { use } = require('use-m');

use('lru-cache@8').then((LRU) => {
  const cache = new LRU({
    max: 100, // Define maximum cache size
  });
  console.log('Cache created with max size of', cache.max);
});

For ESM:

import { use } from 'use-m';

const LRU = await use('lru-cache@8');

const cache = new LRU({
  max: 100, // Define maximum cache size
});
console.log('Cache created with max size of', cache.max);

To install use-m itself, use: npm i use-m or yarn add use-m.

P.S.

Last version that I found works with LRU as constructor is 8. Versions 9-11 produce Uncaught TypeError: LRU is not a constructor.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论