简体   繁体   English

Node.js - 要求未定义

[英]Node.js - require is not defined

I'm trying to import the mysql driver in node.js but I keep getting this error:我正在尝试在 node.js 中导入 mysql 驱动程序,但我不断收到此错误:

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导致问题的文件称为database.js ,我试图将其导入script.js使用

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

This is how I am importing script.js in HTML:这就是我在 HTML 中导入script.js的方式:

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

I'm using Node.js 12.18.0我正在使用 Node.js 12.18.0

You cant use CommonJs (require) in the browser, it is for Node.js.您不能在浏览器中使用 CommonJs (require),它适用于 Node.js。

You cant, or should not be able to use mysql stuff on the client side, this would be a big security issue.您不能或不应该能够在客户端使用 mysql 的东西,这将是一个很大的安全问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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