Ruby on Rails引入SCSS失败:使用Bootstrap Gem启动报错求助
Hey there, let's work through this ExecJS error you're facing when trying to use Bootstrap in your Rails app. That identifier '(function(opts, pluginOpts) {return eval(process' undefined error is usually tied to issues with the JavaScript runtime ExecJS relies on, or small hiccups in your asset pipeline setup. Here's how to fix it step by step:
1. Check your Node.js version (most common fix)
ExecJS uses your system's Node.js to process JavaScript assets, and Ruby 2.4.x + Rails 5.2 don't play nice with newer Node.js versions (16+).
- Run
node -vin your terminal to check your current version. - If it's 16 or higher, downgrade to a compatible version like Node.js 14.x (LTS). You can use a version manager like
nvmto switch versions easily:nvm install 14 nvm use 14
2. Clean up your asset pipeline cache
Stale cached assets can cause weird errors like this. Run these commands to clear and rebuild your assets:
rails assets:clobber rails assets:precompile # Skip this in development if you use live asset compilation
3. Verify your Bootstrap import setup
Double-check your application.scss file to make sure there's no invalid syntax messing things up:
- Ensure the file is named
application.scss(not.css) so Rails processes it as SCSS. - The
@import "bootstrap";line should be placed after any comment blocks (like you have), but make sure there are no stray characters or invalid SCSS before it that could break the import.
4. Fall back to therubyracer (if Node.js fixes don't work)
If adjusting Node.js doesn't resolve the issue, you can replace ExecJS's default runtime with the V8 engine via the therubyracer gem:
- Add this line to your Gemfile:
gem 'therubyracer', platforms: :ruby - Run
bundle installto install it, then restart your Rails server.
Quick checks on your existing code
Looking at your files:
- Your
guest.erblayout is correctly linking to theapplicationstylesheet—no issues there. - Your Gemfile has all the right dependencies (
sass-rails,bootstrap4.1.1 is fully compatible with Rails 5.2) so that's good to go.
Try these steps in order, and your Bootstrap styles should load without that ExecJS error soon!
内容的提问来源于stack exchange,提问作者Matheus Souza

