简体   繁体   中英

Node.js - require is not defined

I'm trying to import the mysql driver in node.js but I keep getting this error:

Uncaught ReferenceError: require is not defined

This is the line that is causing the issue:

//database.js
const mysql = require("mysql");

The file that is causing the issue is called database.js which I am trying to import into script.js using

//script.js
import {myFunction} from "../database.js";

This is how I am importing script.js in HTML:

<script type="module" src="script.js"></script>

I'm using Node.js 12.18.0

You cant use CommonJs (require) in the browser, it is for Node.js.

You cant, or should not be able to use mysql stuff on the client side, this would be a big security issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM