Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Multiple Errors when requiring files #278

Open
TrevorThomas4 opened this issue Aug 1, 2018 · 0 comments
Open

Multiple Errors when requiring files #278

TrevorThomas4 opened this issue Aug 1, 2018 · 0 comments

Comments

@TrevorThomas4
Copy link

TrevorThomas4 commented Aug 1, 2018

I am trying to use requirejs-rails in my rails project and I am running into problems with it. I have added the gem and ran bundle install, but I am unable to actually require a file. My relevant files are:

application.js

// = require require
require(['jquery'], function($) {
    'use strict';

    // Your awesome javascript goes here!

    // **********************************
    // This is just some guff to help you prove that everything works.
    // You can and should delete this once everything works.
    console.log('Successfully loaded jQuery version: ' + $('body').jquery);
    if (typeof $.rails === 'object') {
        console.log('The $ object seems to have been decorated by Rails UJS successfully.');
    }
    else {
        console.log('Uh-oh, it seems that $ has not been decorated by Rails UJS. You might want to check this.');
    }
    // **********************************

});

head of application.html.erb

<head>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= requirejs_include_tag "require" %>
    <%= csrf_meta_tags %>
  </head>

Then where I am trying to use it:

var antlr4 = require(['foo/antlr4/index']); 
var input = 'foobar';
var chars = new antlr4.InputStream(input);

My app/assets/javascripts/foo/antlr4/index.js looks like the following

exports.foo = require('./foo');
exports.bar = require('./bar')

and it gives me ReferenceError: Can't find variable: exports and TypeError: undefined is not a constructor (evaluating 'new antlr4.InputStream(input)')

If I change the head of application.html.erb, to the following:

<head>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'require.js' %>
    <%= requirejs_include_tag "application" %>
    <%= csrf_meta_tags %>
  </head>

It just gives me ReferenceError: Can't find variable: exports. Can anybody help with this issue? I might of included things incorrectly

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant