未捕获的ReferenceError:未定义require / var fs = require('fs');

时间:2019-08-15 09:49:54

标签: javascript

由于文件系统模块,我想读取文本文件。但是,我有一个问题是这条线:     var fs = require('fs');

我的代码很简单,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Eatology Maps</title>
    <style>
        #map{
            height:400px;
            width:100%;
        }
    </style>

</head>

<h1>My Google Map</h1>
<div id="map"></div>

<script>

var fs = require('fs');

fs.readFile('my-file.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});

    function initMap(){
        // Map options
        var options={
            zoom:11,
            center:{lat:22.3193,lng:114.1694}
        }

        // New map
        var map=new google.maps.Map(document.getElementById('map'),options);

        arrayLocation=[];

        if (navigator.geolocation) { //check if geolocation is available
            navigator.geolocation.getCurrentPosition(function(position){
                arrayLocation.push([position.coords.latitude,position.coords.longitude]);

                console.log(arrayLocation);


                var coords=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                var marker= new google.maps.Marker({map:map,position:coords});
            });   
        }

}


    </script>
    <script async defer
     src="https://maps.googleapis.com/maps/api/js? 
   key=AIzaSyDbLwypHTH5Or7M4y_aFSvrmPfX-ANuRR8&callback=initMap">
     </script>
    </body>
    </html>

我在node_modules中上传了文件系统,这是我的文件夹的放置方式:

transport-tracker-location :
    public :
        index.html
    node_modules :
        file-system

我该如何解决?

非常感谢!

0 个答案:

没有答案