简体   繁体   English

要求没有定义? Node.js

[英]require is not defined? Node.js

Just started working with Node.js.刚开始使用 Node.js。 In my app/js file, I am doing something like this:在我的app/js文件中,我正在做这样的事情:

app.js应用程序.js

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Am I really running a server?!');
}).listen(8080, '127.0.0.1');

console.log('running server!');

When I'm in my terminal and run node app.js , the console spits out 'running server!'当我在终端中运行node app.js时,控制台会吐出'running server!' , but in my browser I get, Uncaught ReferenceError: require is not defined . ,但在我的浏览器中,我得到了Uncaught ReferenceError: require is not defined

Can someone explain to me why in the terminal, it works correctly but in the browser, it doesn't?有人可以向我解释为什么在终端中它可以正常工作,但在浏览器中却不能吗?

I am using the node's http-server to serve my page.我正在使用节点的http-server来服务我的页面。

This can now also happen in Node.js as of version 14.从版本 14 开始,这现在也可以在 Node.js 中发生。

It happens when you declare your package type as module in your package.json .当您在package.json中将包类型声明为模块时会发生这种情况。 If you do this, certain CommonJS variables can't be used , including require .如果您这样做, 则不能使用某些 CommonJS 变量,包括require

To fix this, remove "type": "module" from your package.json and make sure you don't have any files ending with .mjs .要解决此问题,请从package.json删除"type": "module"并确保您没有任何以.mjs结尾的文件。

In the terminal, you are running the node application and it is running your script.在终端中,您正在运行节点应用程序并且它正在运行您的脚本。 That is a very different execution environment than directly running your script in the browser.这是一个与在浏览器中直接运行脚本截然不同的执行环境。 While the Javascript language is largely the same (both V8 if you're running the Chrome browser), the rest of the execution environment such as libraries available are not the same.虽然 Javascript 语言大致相同(如果您运行的是 Chrome 浏览器,则两者都是 V8),但其余的执行环境(例如可用的库)并不相同。

node.js is a server-side Javascript execution environment that combines the V8 Javascript engine with a bunch of server-side libraries. node.js 是一个服务器端 Javascript 执行环境,它结合了 V8 Javascript 引擎和一堆服务器端库。 require() is one such feature that node.js adds to the environment. require()是 node.js 添加到环境中的一项功能。 So, when you run node in the terminal, you are running an environment that contains require() .因此,当您在终端中运行 node 时,您正在运行一个包含require()的环境。

require() is not a feature that is built into the browser. require()不是浏览器内置的功能。 That is a specific feature of node.js, not of a browser.这是 node.js 的一个特定特性,而不是浏览器的特性。 So, when you try to have the browser run your script, it does not have require() .因此,当您尝试让浏览器运行您的脚本时,它没有require()

There are ways to run some forms of node.js code in a browser (but not all).有多种方法可以在浏览器中运行某些形式的 node.js 代码(但不是全部)。 For example, you can get browser substitutes for require() that work similarly (though not identically).例如,您可以获得类似(尽管不完全相同)的require()浏览器替代品。

But, you won't be running a web server in your browser as that is not something the browser has the capability to do.但是,您不会在浏览器中运行 Web 服务器,因为浏览器无法做到这一点。


You may be interested in browserify which lets you use node-style modules in a browser using require() statements.您可能对browserify感兴趣,它允许您使用require()语句在浏览器中使用节点样式模块。

Node.JS is a server-side technology, not a browser technology. Node.JS 是一种服务器端技术,而不是浏览器技术。 Thus, Node-specific calls, like require() , do not work in the browser.因此,特定于节点的调用,如require() ,在浏览器中不起作用。

See browserify or webpack if you wish to serve browser-specific modules from Node.browserify的WebPack如果你想从节点服务的浏览器专用模块。

As Abel said, ES Modules in Node >= 14 no longer have require by default.正如 Abel 所说, Node >= 14 中的ES 模块默认不再require

If you want to add it, put this code at the top of your file:如果要添加它,请将此代码放在文件的顶部:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

Source: https://nodejs.org/api/modules.html#modules_module_createrequire_filename来源: https : //nodejs.org/api/modules.html#modules_module_createrequire_filename

just remove "type":"module" from your package.json.只需从 package.json 中删除 "type":"module"。 I hope this will help you😊.我希望这会帮助你😊。

My mistake: I installed ESLint to my project and made a mistake when I filled out the questionnaire and chose wrong type of modules) Maybe it will be helpful for someone))我的错误:我在我的项目中安装了 ESLint,并且在填写问卷时出错并选择了错误的模块类型)也许它会对某人有所帮助))

What type of modules does your project use?您的项目使用什么类型的模块? · * commonjs · * commonjs

Point 1: Add require() function calling line of code only in the app.js file or main.js file. Point 1:仅在app.js文件或main.js文件中Add require()函数调用代码行。

Point 2: Make sure the required package is installed by checking the pacakage.json file.第 2 点:通过检查pacakage.json文件确保安装了所需的包。 If not updated, run "npm i".如果未更新,请运行“npm i”。

I solve this by doing this steps:-我通过执行以下步骤解决了这个问题:-

step 1: create addRequire.js file at the project root.第 1 步:在项目根目录创建addRequire.js文件。

step 2: inside addRequire.js add this lines of code第 2 步:在addRequire.js添加这行代码

import { createRequire } from "module";
const require = createRequire(import.meta.url);

global.require = require; //this will make require at the global scobe and treat it like the original require

step 3: I imported the file at the app.js file head第 3 步:我在app.js文件头导入了文件

import "./start/addRequire.js";

now you have require beside import and you can use it both.现在你在 import 旁边有 require 并且你可以同时使用它。

I solve it, by removing below line from package.json file我通过从 package.json 文件中删除以下行来解决它

 "type": "module"

Hope it will solve the problem.希望它能解决问题。

To supplement what everyone else has said above, your js file is being read on the client side when you have a path to it in your HTML file.为了补充上面其他人所说的内容,当您的 HTML 文件中有一个路径时,您的 js 文件就会在客户端被读取。 At least that was the problem for me.至少这对我来说是个问题。 I had it as a script in my tag in my index.html Hope this helps!我在 index.html 的标签中将其作为脚本希望这会有所帮助!

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

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