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

Multiple Errors when requiring files #278

@TrevorThomas4

Description

@TrevorThomas4

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions