Reputation: 49
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 © <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
Upvotes: 1
Views: 3407
Reputation: 71
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)
# ...
Upvotes: 1