解决错误未捕获的ReferenceError:Node.js中未定义require

While working with Node JS, you might be familiar with the require() and sometimes, it shows error like Uncaught ReferenceError: require is not defined.

在使用Node JS时,您可能熟悉require() ,有时,它会显示类似未捕获的ReferenceError的错误:require未定义。

So why does this happens? Let’s see in today’s post.

那么为什么会这样呢? 让我们看看今天的帖子。

Before coming to the error, let’s know what is require() used for.

在出现错误之前,让我们知道require()的用途。

有什么要求? (What is require?)

The require function is the builtin function of node js that helps us to include local or node_modules in our project that exists in a separate file.

require函数是node js的内置函数,可帮助我们在单独文件中的项目中包含本地或node_modules。

1
2
3
4
5
6
7
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log('App is running!'))
1
2
3
4
5
6
7
const express = require ( 'express' )
const app = express ( )
const port = 3000
app . get ( '/' , ( req , res ) = > res . send ( 'Hello World!' ) )
app . listen ( port , ( ) = > console . log ( 'App is running!' ) )

Here, we have imported express module into our code using require('express'). The require function will look for files in builtin core modules, NPM modules(node_modules), local modules, etc.

在这里,我们使用require('express')将express模块​​导入了我们的代码。 require函数将在内置核心模块,NPM模块(node_modules),本地模块等中查找文件。

require is not defined

Now, let’s see what is the reason for the error. Here is an example code.

现在,让我们看看错误的原因是什么。 这是示例代码。

Serve this code using node’s http-server and it will show server running in the console but when you will fire it in the browser, you’ll get Uncaught ReferenceError: require is not defined.

使用节点的http-server服务此代码,它将显示服务器在控制台中运行,但是当您在浏览器中将其触发时,会出现Uncaught ReferenceError: require is not defined

现在,为什么会这样呢? (Now, why does this happened?)

The require function is defined only in node js and it has nothing to do with the browser. Both Node JS and chrome has Javascript’s V8 engine but both of them are completely different in terms of running js. Your browser is capable of running only client-side javascript. To run this code without any kind of error, you need to install node on your system. Here is a detailed guide on installing node js on Mac, Windows or Linux.

require函数仅在节点js中定义,与浏览器无关。 Node JS和chrome都具有Javascript的V8引擎,但是它们在运行js方面完全不同。 您的浏览器只能运行客户端javascript。 要运行此代码而没有任何错误,您需要在系统上安装节点。 这是有关在Mac,Windows或Linux上安装Node js的详细指南。

Once installed, fire a terminal and type node -v to check if it is successfully installed or not.

一旦安装,消防终端输入node - v ,以检查其是否安装成功与否。

To run this code without error, save this file as app.js and run it in your terminal using the command node app.js The code will show the output in the terminal server running. This means your code is running without any problem.

要无错误地运行此代码,请将此文件另存为app.js并使用node app.js在终端中server running该代码。该代码将显示server running的终端server running的输出。 这意味着您的代码正在运行,没有任何问题。

Well, this was all about the function require() and the error related to it. If you stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug on your own as someone might have faced a similar problem earlier.

好吧,这一切都与功能require()及其相关的错误有关。 如果您遇到任何类型的错误,请别忘了用Google搜索并尝试自行调试。 这将教您如何自行调试,因为之前可能有人遇到过类似的问题。

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.

如果您仍然找不到解决问题的方法,可以在下面的评论部分中提出疑问,我们会尽快与您联系🤓。

翻译自: https://www.thecrazyprogrammer.com/2020/05/require-is-not-defined.html