简体   繁体   English

Node JS:ReferenceError:require未定义

[英]Node JS: ReferenceError: require is not defined

I want to use MySQL database. 我想使用MySQL数据库。 I installed MySQL with command npm i mysql . 我用命令npm i mysql安装了MySQL。 In order to use it, I wrote: 为了使用它,我写道:

var mysql = require('mysql');

But when I run the program, it shows ReferenceError: require is not defined error. 但是当我运行程序时,它显示ReferenceError:require未定义错误。

I wrote this line in home.ejs file within a script tag. 我在脚本标记中的home.ejs文件中写了这一行。

Home.ejs isn't the approriate file to write this line in. ejs files won't contains that much logic (except condition and loop over some element in your dom). Home.ejs不是写这行的approriate文件.ejs文件不会包含那么多逻辑(除了条件和循环在你的dom中的某些元素)。 Basically what you want to do is anodeJs script file which will connect to mysql, handle the request and serve your ejs files with your data. 基本上你要做的是阳极的脚本文件,它将连接到mysql,处理请求并用你的数据提供你的ejs文件。

Using express you'll have something like this in your node file : 使用express你会在你的节点文件中有这样的东西:

app.get("/home",(req,res)=>{ res.render("home.ejs", {data : data}) (where data is what you get from your DB app.get("/home",(req,res)=>{ res.render("home.ejs", {data : data}) (其中数据来自数据库)

By default require() is not a valid function in client side javascript. 默认情况下,require()不是客户端javascript中的有效函数。 I recommend you look into require.js as this does extend the client side to provide you with that function. 我建议您查看require.js,因为这会扩展客户端以为您提供该功能。

you need to do your mysql processing before you get to the ejs template. 你需要在进入ejs模板之前进行mysql处理。 Right now, you are trying to use require in a template, which is being rendered on the browser. 现在,您正在尝试在模板中使用require ,该模板正在浏览器上呈现。 You won't be able to do that without using a module loader like requirejs . 如果不使用像requirejs这样的模块加载器,您将无法做到这一点。

require() by default is a NodeJS function on the server side. require()默认是服务器端的NodeJS函数。 You can use require.js like Abhilash said. 你可以使用像Abhilash这样的require.js。 It's bad practice to have mysql in the browser. 在浏览器中使用mysql是不好的做法。 Your username, password, and host will be exposed to the world. 您的用户名,密码和主机将向全世界公开。

The browser does not have an API that require modules. 浏览器没有require模块的API。 Also, this is something that you would do in a nodejs application (in the backend NOT the front end), typically in a .js file. 此外,这是您在nodejs应用程序(在后端而不是前端)中执行的操作,通常在.js文件中。

暂无
暂无

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

相关问题 未捕获的ReferenceError:未定义require-Node.js - Uncaught ReferenceError: require is not defined - Node.js 节点:ReferenceError:未定义要求 - Node: ReferenceError: require is not defined 在Node.js中解决“Uncaught ReferenceError:require is not defined”错误 - Resolve “Uncaught ReferenceError: require is not defined” error in Node.js 未捕获的 ReferenceError: require 未定义。 Visual Studio 中的 Node.js - Uncaught ReferenceError: require is not defined. Node.js in Visual Studio Node.js 上的客户端:未捕获 ReferenceError:未定义要求 - Client on Node.js: Uncaught ReferenceError: require is not defined 从node.js开始获取错误Uncaught ReferenceError:模块未定义,而Uncaught ReferenceError:require未定义 - Starting off in node.js getting errors Uncaught ReferenceError: module is not defined, and Uncaught ReferenceError: require is not defined 流星/ node-gd和ReferenceError:需求未定义 - Meteor/node-gd & ReferenceError: require is not defined Javascript / Node / Twilio-ReferenceError:未定义require - Javascript/Node/Twilio - ReferenceError: require is not defined 将 Node.js SDK 导入在节点服务器中运行的 Typescript 中出现错误“ReferenceError:require is not defined” - Importing Node.js SDK into Typescript running in Node server gave error “ReferenceError: require is not defined” ReferenceError: require is not defined (at node:internal/main/run_main_module:23:47) 在 Node.js - ReferenceError: require is not defined (at node:internal/main/run_main_module:23:47) in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM