2015-11-20 77 views
1

部署部署我在瀏覽器控制檯有以下錯誤之前工作後:軌道4 - JavaScript沒有在Heroku

ReferenceError: require is not defined npm.self-...js:2:0 

儘管這樣,我的本地服務器上的JavaScript運行完美。當我在heroku上部署我的應用程序時,它不會,但是css會,所以我不確定資產管道引起的問題。我有

group :production do 
    gem 'pg' 
    gem 'rails_12factor' 
end 

在我的Gemfile中。我試圖在本地預編譯資產,甚至通過「heroku run」命令,但它沒有改變任何東西。這裏是我的application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// compiled file. 
// 
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 
// about supported directives. 
// 
//= require jquery 
//= require jquery_ujs 
//= require_tree . 
//= require moment 
//= require bootstrap-datetimepicker 

$(function() { 
    $("#datetimepicker").datetimepicker({ 
    locale: "en", 
    format: "lll" 
    }); 
}); 

$(function() { 
    $("#events").on("click", ".pager a", function() { 
    $.getScript(this.href); 
    return false; 
    }); 
}); 

而且npm.js:

// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment. 
require('../../js/transition.js') 
require('../../js/alert.js') 
require('../../js/button.js') 
require('../../js/carousel.js') 
require('../../js/collapse.js') 
require('../../js/dropdown.js') 
require('../../js/modal.js') 
require('../../js/tooltip.js') 
require('../../js/popover.js') 
require('../../js/scrollspy.js') 
require('../../js/tab.js') 
require('../../js/affix.js') 

如果你要問我可以共享GitHub的鏈接。謝謝!

+0

你有預編譯資產嗎? –

+0

@GaganGami是的,但預編譯似乎不起作用。 –

回答

0

對不起,沒有足夠的聲望評論,否則會有。

require()函數沒有內置到瀏覽器中。我建議嘗試刪除

//= require_tree . 

&手動添加JavaScript文件(僅測試,你沒有得到的錯誤)這樣

//= require desired_js_file 

//= require_tree .預編譯一切asset/javascript的根服務於http請求。當瀏覽器遇到js文件時require()function它可能會給你錯誤。 希望這有助於!

+0

感謝您的回答,但我遇到了application.js文件本身的問題。如果你看上面的這個文件,你會在文件末尾看到兩個功能。所以這些功能不起作用。 –