在部署之前,我在浏览器控制台中遇到以下错误:

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链接。谢谢!

最佳答案

抱歉,没有足够的声誉来发表评论,否则将会发表评论。

require()函数未内置在浏览器中。我建议尝试删除

//= require_tree .


&手动添加javascript文件(仅用于测试,不会收到错误),就像这样

//= require desired_js_file


//= require_tree .预编译asset/javascript根目录中的所有内容,并用于http请求。当浏览器遇到带有js require()function文件时,可能会给您错误。
希望这对您有所帮助!

关于javascript - Rails 4-在Heroku上部署后,Javascript不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33822136/

10-09 18:04
查看更多