未捕获的 ReferenceError:未定义进程 [英] Uncaught ReferenceError: process is not defined

查看:25
本文介绍了未捕获的 ReferenceError:未定义进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node.js 创建一个 Web 应用程序.当我运行应用程序时(通过在浏览器上打开 index.html 或在终端上使用命令npm start")我得到两个错误:

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:

未捕获的引用错误:未定义进程

Uncaught ReferenceError: process is not defined

未捕获的 ReferenceError: require 未定义

Uncaught ReferenceError: require is not defined

我通过在我的 index.html head 标签中特别包含到 this 脚本,其中定义了 require 函数.但是,我找不到与流程功能类似的东西.

I solved the "require is not defined" error by specifically including in my index.html head tag the link to this script, where the require function is defined. However, I cannot find something similar for the process function.

我的问题是双重的:

  1. 为什么需要重新定义内置的 node.js 模块?为什么它们不被认可,即内置模块"?术语内置模块"是不是意味着模块不需要在外部/二手重新定义?

  1. 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?

有没有办法解决这个问题?我的脚本很简单,我只是尝试使用node.js的一个基本功能,所以我无法弄清楚我可能犯了什么错误.

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.

如果有人遇到过这个问题,并找到了解决方法或发生这种情况的原因,那么您会很有帮助.

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.

推荐答案

Node.js 代码必须由节点进程运行,而不是浏览器(代码必须在服务器中运行).

Node.js code must be run by the node process, not the browser (the code must run in the server).

要运行代码,您必须运行以下命令:

To run the code, you must run the command:

node server.js

然后你就可以从浏览器访问你的服务器了,例如输入http://localhost:8080".您必须有一个包含您想要的服务器代码的文件 server.js(或其他)(在本例中,在端口 8080 中创建一个 Web 服务器).

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).

您可以遵循这个简单的示例,使用 express 作为 http 服务器模块:http://expressjs.com/starter/hello-world.html

You can follow this easy example, using express as http server module: http://expressjs.com/starter/hello-world.html

这篇关于未捕获的 ReferenceError:未定义进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆