需要未定义 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48114749/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:49:03  来源:igfitidea点击:

require is not defined javascript

javascriptnode.jsgoogle-chromefirefox

提问by JakeTheDane

****** EDIT *******

****** 编辑 *******

I do realize the mistake now, i was running my script in my terminal when i was testing my require.

我现在意识到了这个错误,当我测试我的需求时,我正在我的终端中运行我的脚本。

****** OLD ****

****** 老的 ****

The answer might all ready be here on the page, but i have not been able to find it, the general answers i see is that i cannot use "require" client sided.

答案可能都在页面上准备好了,但我还没有找到它,我看到的一般答案是我不能在客户端使用“require”。

The thing is the script i am trying to run works perfectly on my school laptop. But it doesnt want to run on my on Desktop at home.

问题是我试图在我的学校笔记本电脑上完美运行的脚本。但它不想在家里的桌面上运行。

I am trying to run a script where i want to require like this. "var fs = require('fs');"

我正在尝试运行一个我想要这样要求的脚本。"var fs = require('fs');"

But the console in my browser just gives me this error ( ReferenceError: require is not )

但是我浏览器中的控制台只是给了我这个错误( ReferenceError: require is not )

I am using Visual Studio Code, with Node.js.

我正在使用 Visual Studio Code 和 Node.js。

I have a basic index.html file which uses 1 script.js file. Nothing else is being used for it.

我有一个基本的 index.html 文件,它使用 1 个 script.js 文件。没有其他东西被用于它。

A link for my files should you want to take a look ( https://drive.google.com/file/d/1VgEmwtcHkURFT1WmPOnEKr_OHLT_SY78/view?usp=sharing)

如果您想查看我的文件的链接( https://drive.google.com/file/d/1VgEmwtcHkURFT1WmPOnEKr_OHLT_SY78/view?usp=sharing

采纳答案by Quentin

I am using Visual Studio Code, with Node.js.

I have a basic index.html file which uses 1 script.js file.

我正在使用 Visual Studio Code 和 Node.js。

我有一个基本的 index.html 文件,它使用 1 个 script.js 文件。

So you have two sets of JS.

所以你有两套JS。

  1. JS that runs in Node.js
  2. JS that runs in the browser (delivered via index.html)
  1. 在 Node.js 中运行的 JS
  2. 在浏览器中运行的 JS(通过 index.html 提供)

var fs = require('fs');is very much in the former category. requireis a built-in in Node.js. fsis a core library of Node.js.

var fs = require('fs');非常属于前一类。require是 Node.js 内置的。fs是 Node.js 的核心库。

You are trying to run it in the browser, where it isn't supported and does not work. You need to run it in Node.js.

您正在尝试在浏览器中运行它,但它不受支持且不起作用。您需要在 Node.js 中运行它。

i see is that i cannot use "require" client sided.

我看到的是我不能使用“需要”客户端。

There are browser-side implementations of require, but they won't help you here because fsdepends on Node.js.

有浏览器端的 实现require,但它们在这里不会帮助你,因为fs依赖于 Node.js。

回答by MstrQKN

require() is not standard JavaScript, it's built into NodeJS to load modules.

require() 不是标准的 JavaScript,它内置于 NodeJS 中以加载模块。

Try using Express or similar to run a simple web server and it should work. Right now it's not working from home because it's not being compiled by the Node engine.

尝试使用 Express 或类似工具运行一个简单的 Web 服务器,它应该可以工作。现在它不能在家工作,因为它不是由 Node 引擎编译的。

回答by Ivo Facundo

require() it's not an internal function of javascript in the front-end, you're going to need to import some library that realizes this for you, as for example require.js or browserify. Otherwise fs is only supported in NodeJS you need to run in the server not in the browser.

require() 它不是前端javascript的内部函数,您将需要导入一些为您实现这一点的库,例如require.js或browserify。否则 fs 仅在 NodeJS 中受支持,您需要在服务器中运行而不是在浏览器中运行。