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.
3 Answers
Reset to default 1If 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
.