question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

asset pipeline - cannot import component: "require is not defined" when using 'import' or 'require'

See original GitHub issue

Steps to reproduce

  1. make a rails project
  2. include react-rails in it via the asset-pipeline way
  3. import Component from './Component'
  4. notice ReferenceError: require is not defined

Expected behavior

Tell us what should happen

it should just import the component

Actual behavior

Tell us what happens instead

i get ReferenceError: require is not defined

System configuration

Sprockets or Webpacker version: sprockets (3.7.1) / sprockets-rails (3.2.1, 3.2.0) React-Rails version: react-rails (2.4.3) Rect_UJS version: don’t know how to find this out Rails version: Rails 5.0.5 Ruby version: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]


  1. How can I use react-rails asset pipeline and import components from folders? Currently I just have assets/javascripts/components/<Component.jsx> referenced like <%= react_component("Component") %>, but I’d like to nest them into folders.

  2. How do I import JS libraries with import instead of putting the browser versions of libraries into assets/javascripts and referencing them in application.js?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
BookOfGregcommented, Aug 27, 2018

If you’re using the default asset pipeline then you need to be importing code the sprockets way. https://guides.rubyonrails.org/asset_pipeline.html#search-paths

For example, these files:

app/assets/javascripts/home.js
lib/assets/javascripts/moovinator.js
vendor/assets/javascripts/slider.js
vendor/assets/somepackage/phonebox.js

would be referenced in a manifest like this:

//= require home
//= require moovinator
//= require slider
//= require phonebox

You can only use Require and Import when using Webpack/Webpacker

2reactions
BookOfGregcommented, Mar 21, 2018

Thanks @RiccardoMargiotta , Yep that’s the answer to this one. Let me know if it needs to be re-opened.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - require is not defined - Stack Overflow
You're trying to use a CommonJS module from within your browser. This will not work. How are you using them? When you write...
Read more >
How to fix the error `require is not defined` in Node.js
It means that the default way of using npm modules is not require anymore, but import . Solution. To get rid of the...
Read more >
The Asset Pipeline - Ruby on Rails Guides
When using Sprockets directives, Sass files exist within their own scope, making variables or mixins only available within the document they were defined...
Read more >
Parcel
@parcel/core: Cannot resolve 'ract' from '. ... All of this is completely automatic and does not require any work by plugin authors ......
Read more >
Importing images with Webpacker - rossta.net
To reference an image from JavaScript in your Webpacker build, simply import it like any other module. React is not required for this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found