Javascript 未捕获的 ReferenceError:未定义进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30239060/
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
Uncaught ReferenceError: process is not defined
提问by Kantharis
I am using node.js to create a web application. When I run the application (either by opening index.html on the browser or using the command "npm start" on the terminal) I get two errors:
我正在使用 node.js 创建一个 Web 应用程序。当我运行应用程序(通过在浏览器上打开 index.html 或在终端上使用命令“npm start”)时,我收到两个错误:
Uncaught ReferenceError: process is not defined
未捕获的 ReferenceError:未定义进程
Uncaught ReferenceError: require is not defined
未捕获的 ReferenceError:需要未定义
I solved the "require is not defined" error by specifically including in my index.html head tag the link to thisscript, where the require function is defined. However, I cannot find something similar for the process function.
我解决了“require is not defined”错误,具体方法是在我的 index.html head 标签中包含指向这个脚本的链接,其中定义了 require 函数。但是,我找不到与流程功能类似的东西。
My question is doublefold:
我的问题是双重的:
Why do built-in node.js modules need to be re-defined? Why are they not recognized as they are, that is "built-in modules"? Doesn't the term "built-in module" mean that a module need not be redefined externaly/second-handedly?
Is there a way to solve this problem? My script is very simple, I am just trying to use a basic function of node.js, so I cannot figure out what errors I might have done.
为什么需要重新定义内置的 node.js 模块?为什么它们不被认可,即“内置模块”?术语“内置模块”不是意味着模块不需要在外部/二手重新定义吗?
有没有办法解决这个问题?我的脚本很简单,我只是尝试使用 node.js 的一个基本功能,所以我无法弄清楚我可能犯了什么错误。
If anyone has come about this problem and has found a way around it or a reason this happens, you would be of great help.
如果有人遇到过这个问题并找到了解决方法或发生这种情况的原因,那么您将有很大帮助。
采纳答案by greuze
Node.js code must be run by the node process, not the browser (the code must run in the server).
Node.js 代码必须由节点进程运行,而不是浏览器(代码必须在服务器中运行)。
To run the code, you must run the command:
要运行代码,您必须运行以下命令:
node server.js
And then you can access your server from a browser by typing "http://localhost:8080", for example. You must have a file server.js (or whatever) with the server code you want (in this case, creating a web server in port 8080).
然后你就可以从浏览器通过输入“ http://localhost:8080”来访问你的服务器,例如。您必须有一个包含您想要的服务器代码的文件 server.js(或其他)(在本例中,在端口 8080 中创建一个 Web 服务器)。
You can follow this easy example, using express as http server module: http://expressjs.com/starter/hello-world.html
你可以按照这个简单的例子,使用 express 作为 http 服务器模块:http: //expressjs.com/starter/hello-world.html
回答by Zeghra
I had same problem when I tried to do this node js app: https://www.youtube.com/watch?v=mr9Mtm_TRpw
当我尝试执行此节点 js 应用程序时遇到了同样的问题:https: //www.youtube.com/watch?v=mr9Mtm_TRpw
The require in html was reached from a < script> and was undefined, like
html 中的 require 是从 < script> 到达的,并且是未定义的,例如
<script> require('./renderer.js');</script>
I changed it to:
我把它改成:
<script src="./renderer.js"></script>
The process in html script was also undefined. I included the webPreferences: nodeIntegration in the js file:
html 脚本中的进程也未定义。我在 js 文件中包含了 webPreferences: nodeIntegration :
win = new BrowserWindow({
width: 800,
height:600,
icon: __dirname+'/img/sysinfo.png',
webPreferences: {
nodeIntegration: true
}
});
I hope it helped.
我希望它有所帮助。
回答by Oluwatobi Lesi
I had the same problem solved it by going into my .eslintrc.js file to configure my globals variables, adding require and process to the globals variable and setting the corresponding value equal to "writable". Hope it works for you.
我通过进入我的 .eslintrc.js 文件来配置我的 globals 变量,将 require 和 process 添加到 globals 变量并将相应的值设置为等于“writable”,从而解决了同样的问题。希望对你有效。
this link really helped https://eslint.org/docs/user-guide/configuring#specifying-globals
这个链接真的很有帮助 https://eslint.org/docs/user-guide/configuring#specifying-globals

