Javascript 要求没有定义?节点.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31931614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
require is not defined? Node.js
提问by Richard Bustos
Just started working with Node.js. In my app/js
file, I am doing something like this:
刚开始使用 Node.js。在我的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!'
, but in my browser I get, Uncaught ReferenceError: require is not defined
.
当我在终端中运行时node app.js
,控制台会吐出'running server!'
,但在我的浏览器中我得到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
来为我的页面提供服务。
回答by Rob Raisch
Node.JS is a server-sidetechnology, not a browser technology. Thus, Node-specific calls, like require()
, do not work in the browser.
Node.JS 是一种服务器端技术,而不是浏览器技术。因此,特定于节点的调用(如 )require()
在浏览器中不起作用。
See browserifyor webpackif you wish to serve browser-specific modules from Node.
见browserify或的WebPack如果你想从节点服务的浏览器专用模块。
回答by jfriend00
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. require()
is one such feature that node.js adds to the environment. So, when you run node in the terminal, you are running an environment that contains require()
.
node.js 是一个服务器端 Javascript 执行环境,它结合了 V8 Javascript 引擎和一堆服务器端库。 require()
是 node.js 添加到环境中的一项功能。因此,当您在终端中运行 node 时,您正在运行一个包含require()
.
require()
is not a feature that is built into the browser. That is a specific feature of node.js, not of a browser. So, when you try to have the browser run your script, it does not have require()
.
require()
不是浏览器内置的功能。这是 node.js 的一个特定特性,而不是浏览器的特性。因此,当您尝试让浏览器运行您的脚本时,它没有require()
.
There are ways to run some forms of node.js code in a browser (but not all). For example, you can get browser substitutes for require()
that work similarly (though not identically).
有多种方法可以在浏览器中运行某些形式的 node.js 代码(但不是全部)。例如,您可以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 browserifywhich lets you use node-style modules in a browser using require()
statements.
您可能对browserify感兴趣,它允许您使用require()
语句在浏览器中使用节点样式模块。
回答by Abel
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
. If you do this, certain CommonJS variables can't be used, including require
.
当您在package.json
. 如果这样做,某些 CommonJS 变量将无法使用,包括require
.
To fix this, remove "type": "module"
from your package.json
and make sure you don't have any files ending with .mjs
.
要解决此问题,请"type": "module"
从您的package.json
文件中删除并确保您没有任何以.mjs
.
回答by Sowndharya
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文件中添加require()函数调用代码行。
Point 2: Make sure the required package is installed by checking the pacakage.json file. If not updated, run "npm i".
第 2 点:通过检查 pacakage.json 文件确保安装了所需的包。如果未更新,请运行“npm i”。
回答by ttalgihon
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. At least that was the problem for me. I had it as a script in my tag in my index.html Hope this helps!
为了补充上面其他人所说的内容,当您的 HTML 文件中有一个指向它的路径时,您的 js 文件就会在客户端被读取。至少这对我来说是个问题。我在 index.html 的标签中将它作为脚本希望这有帮助!