简体   繁体   English

Rails ReferenceError:需求未定义(fs)

[英]Rails ReferenceError: require is not defined (fs)

I'm having some trouble with a basic thing in coffeescript. 我在coffeescript中的一个基本问题上遇到了麻烦。 I'm trying to read a JSON file but it doesn't works. 我正在尝试读取JSON文件,但是它不起作用。 I'm getting a 我正在

ReferenceError: require is not defined ReferenceError:需求未定义

which points to this line 指向这条线

fs  = require ("fs")

My whole script is this (routes.js.coffee): 我的整个脚本是这样的(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: 我的application.js是这样的:

//= 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 . 如果您确实想在客户端使用require ,那么应该考虑添加require.js以使用Require.js

In this case specifically, it looks like that you're just trying to get some data in a json file dinamically. 具体来说,在这种情况下,您似乎只是想以动态方式获取json文件中的某些数据。 You could try somethiing like this: 您可以尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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