I’m trying to set up a basic full stack web project with a SQL database using node.
I generally prefer to use import, and I’ve been using it for the duration of the project, so I have my type set to module in package.json.
Now that the time has come to connect the front/backend to the database, I’ve installed mysql with npm with the basic npm install mysql
and npm install @types/mysql
. As soon as I’ve tried to run the code, though, it errors out in the sql.createConnection
line. On checking the error, it seems to be a problem with mysql – I’m getting:
ReferenceError: require is not defined
and after checking the details of the error, it belongs to one of the files of node_modules/mysql
trying to require another file.
Anyone know what the deal is here? Is it possible the package trying to be required doesn’t exist and is somehow not getting installed properly by npm? Or is there some reason one of my node_modules is unable to use require? I’ve never ran into this problem before and to be honest, I’ve completely stalled.
EDIT – a better diagnosis
Done more looking into it, I believe it’s an issue with my build system.
The application is primitive, to run I use node server.js
, which is served via Express which calls sendFile
to a basic index.html
. Right now, that HTML file just directly contains a script referencing code I’m bundling with esbundle.
Since installing mysql, trying to build with esbuild would send complains that it couldn’t resolve some of the modules contained in mysql (IE “crypto”, “events”, etc) – modules that are built into node. I added the --platform=node
flag to my esbuild, but I’m guessing that even though I’m using node server.js
, somehow require statements are still getting ignored. This might sound stupid, but does using node to start an express server run that server in a node “environment” / “platform”?
If it’s not obvious, I’m not much experienced with node, npm, or basically anything backend related. I still need help for sure.
EDIT 2 – the code
For reference, here’s a simplified version of my index.html:
<head>
<link rel="stylesheet" href="bundle.css">
</head>
<body>
<div> Test website </div>
</body>
<script src="./bundle.js" defer></script>
And a simplified version of my app.ts
import sql from 'mysql';
const config = { ... } // Contains all the config information needed: 'user', 'password', 'server', 'database', 'port', and 'options'
async function app () {
try {
const connection = await sql.createConnection(config);
connection.connect(e => {});
} catch (e) {
console.log(e);
}
}
app()
On trying to run esbuild src/app.ts --external:fs --bundle --minify --sourcemap --outfile=dist/bundle.js
, I get several errors in the vein of X [ERROR] Could not resolve "~"
, where ~ is the name of a node package. For example:
X [ERROR] Could not resolve "events"
node_modules/mysql/lib/Pool.js:3:29:
3 │ var EventEmitter = require('events').EventEmitter;
╵ ~~~~~~~~
I updated the command to include the platform as node, esbuild src/app.ts --external:fs --bundle --minify --sourcemap --outfile=dist/bundle.js --platform=node
. This allows the program to compile successfully, where I can then run node dist/server.js
and the webpage will load. However, the catch block will return the following error: ReferenceError: require is not defined
with a stack trace. Looking at the first file, I can see it’s the code of the mysql package with a few different require statements, along the lines of var Events = require('events');
.
Steven Shoemaker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1