“需要"未定义错误-javascript [英] "require" is not defined error- javascript
问题描述
运行.html文件时,我在浏览器中收到此错误
I get this error in the browser when I run my .html file
Uncaught ReferenceError: require is not defined
我的File_system.html是:
My File_system.html is:
<!DOCTYPE html>
<html>
<head>
<title>window.requestFileSystem problem</title>
<script type="text/javascript" src="server.js"></script>
<script>
function onInitFs(fs) {
console.log(fs);
console.log(fs.getDirectory());
}
function errorHandler(e) {
var msg = '';
console.log(e);
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.PERSISTENT, 1024*1024,onInitFs,errorHandler);
</script>
</head>
<body>
</body>
</html>
我的server.js文件是:
And my server.js file is:
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('File_system.html');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
}).listen(8080);
这有什么问题?请帮助
我应该能够在控制台中检索所有fs方法,并使用tat创建目录.但是我陷入了困境. 还是有任何方法可以直接在浏览器中运行?
I should be able to retrieve all the fs methods in the console and create a directory with tat. But I am stuck in this. Or is there any way to directly run in browser?
推荐答案
编辑:重新阅读您的原始文章,您可能实际上正在运行此Node.js.节点环境中的脚本,尝试使用fs
加载.html文件.如果是这样,您就是在.html文件中的 内调用Node server.js脚本,如下所示:
Rereading your original post, it's possible that you are in fact running this Node.js script in a Node environment, trying to use fs
to load your .html file. If that's the case, you are calling your Node server.js script within your .html file like so:
<script type="text/javascript" src="server.js"></script>
只需删除此行,您就不会再收到所看到的错误.
Simply remove this line, and you should no longer receive the error you're seeing.
如前所述,您正在尝试在客户端脚本中实现 Node.js 代码.从理论上讲,可以完成,但是不建议这样做,而Node.js根本不建议这样做为此目的而设计(它是一台服务器!)
As previously stated, you're attempting to implement Node.js code in your client-side script. In theory this can be done, but isn't recommended, and Node.js simply isn't designed for this purpose (it's a server!)
您应该使用 XHR/ajax
从客户端向服务器发出http请求(而不是fs
),然后在后端处理这些事情.可以使用传统的Web堆栈(例如Apache和php)轻松完成此操作,或者如果您确实要使用fs
,请从Node.js运行API,然后使用 Sails .js .
You should be using XHR/ajax
to make http requests from your client to a server (not fs
), and then handle those things on the back end. This can be done easily with a conventional web stack like Apache and php, or if you really want to use fs
, run an API off of Node.js and connect your client to it using XHR/ajax
, or implement an MVC stack like Sails.js.
这篇关于“需要"未定义错误-javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!