简体   繁体   中英

Rails ReferenceError: require is not defined (fs)

I'm having some trouble with a basic thing in coffeescript. I'm trying to read a JSON file but it doesn't works. I'm getting a

ReferenceError: require is not defined

which points to this line

fs  = require ("fs")

My whole script is this (routes.js.coffee):

loadFiles = ->
  fs  = require "fs"
  fs.readFile 'A4.json', (err, geoData) -> fileText = geoData

ready = ->
  geoData = loadFiles() #Guess this line is wrong but I need to focus on the other error
  map = L.map('map').setView([5.81107293, -73.030279174], 13)

  L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?  access_token={accessToken}', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
#Some irrelevant code...
}).addTo(map);

$(document).ready(ready)
$(document).on('page:load', ready)

I've been reading other related questions posted here but the suggested solutions haven't worked for me (order the imports). My application.js is this:

//= require jquery
//= require jquery_ujs
//= require leaflet
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

Thanks in advance

If you really want to use require at the client-side then you should consider to add require.js to use Require.js .

In this case specifically, it looks like that you're just trying to get some data in a json file dinamically. You could try somethiing like this:

ready = ->
  $.get "A4.json", (geoData)->
    fileText = geoData
    map = L.map('map').setView([5.81107293, -73.030279174], 13)
    # ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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