尝试从javascript中的文本文件读取时遇到错误。
这是代码
<!Doctype html>
<html>
<head>
Dwell time for workers
</head>
<script src = "https://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript" src="workers_dwell_time_final1.json"></script>
<script type = "text/javascript"></script>
<script>
//var mydata = JSON.parse(data);
//console.log(mydata[0].name);
//console.log(mydata[0].time);
// <script src = "https://requirejs.org/docs/release/2.3.5/minified/require.js" > /script>
const fs = require('fs')
// Reading data in utf-8 format
// which is a type of character set.
// Instead of 'utf-8' it can be
// other character set also like 'ascii'
fs.readFile('myfile.txt', 'utf-8', (err, data) => {
if (err) throw err;
// Converting Raw Buffer to text
// data using tostring function.
console.log(data);
})
</script>
<body>
<h1> worker time : </h1>
</body>
</html>
这是错误
require.js:168 Uncaught Error: Module name "fs" has not been loaded yet for context: _. Use require([])
https://requirejs.org/docs/errors.html#notloaded
at makeError (require.js:168)
at Object.localRequire [as require] (require.js:1436)
at requirejs (require.js:1797)
at Dashboard.html:18
我该如何删除错误。如果有人拥有适当的require.js文件,请共享它
答案 0 :(得分:2)
您要执行的操作是加载未与应用程序捆绑在一起的模块。而且我也不认为您可以这样阅读。不在网页中(与nodejs相比)。您需要从服务器(该文件是公用文件夹,请参见:https://www.w3schools.com/html/html_filepaths.asp)中的服务器提供该文件。