简体   繁体   English

NodeJs 要求未定义

[英]NodeJs require is not defined

I'm trying for the first time NodeJs, I installed NodeJs, npm and all needed modules for my project.我第一次尝试 NodeJs,我为我的项目安装了 NodeJs、npm 和所有需要的模块。 Now I want to run it on my website (still on localhost).现在我想在我的网站上运行它(仍然在本地主机上)。 But when I open console there is error saying Uncaught ReferenceError: require is not defined at HTMLDocument.但是当我打开控制台时,出现错误说Uncaught ReferenceError: require is not defined at HTMLDocument。 And then specification on which line in code I used require.然后指定我使用的代码中的哪一行需要。 It is simple line const bodyParser = require("body-parser");这是简单的行const bodyParser = require("body-parser"); but on the website it does not work.但在网站上它不起作用。 Is there something more I need to do?我还需要做些什么吗?

require is only used from server-side and not usable on browser. require仅在服务器端使用,不能在浏览器上使用。 And please know that NodeJs is JavaScript RunTime for server-side programming.并且请知道NodeJs是 JavaScript RunTime 用于服务器端编程。

yash is right.亚什是对的。 If you want to import a module on front-end side, you can use ES6's import .如果要在前端导入模块,可以使用 ES6 的import All modern browsers support it.所有现代浏览器都支持它。 But if you want to import in oldest browsers too, read about browserify where you can use special require function.但是,如果您也想在最旧的浏览器中导入,请阅读browserify ,您可以在其中使用特殊require function。

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

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