Node_Module is trying to use require (mysql)

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'); .

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật