Error: Cannot find module '@rails/webpacker' rails 6
Asked Answered
A

1

8

When I run my Rails 6.0 application on development environment I get the next trace on the development.log

[Webpacker] Compiling…
[Webpacker] Compilation failed:
/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:93
                throw err;
                ^

Error: Cannot find module '@rails/webpacker'
Require stack:
- /mnt/config/webpack/environment.js
- /mnt/config/webpack/development.js
- /home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js
- /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js
- /home/alejo/rails_project/node_modules/webpack/bin/webpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
    at Function.Module._load (internal/modules/cjs/loader.js:847:27)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at Object.<anonymous> (/mnt/config/webpack/environment.js:1:87)
    at Module._compile (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32)
    at Function.Module._load (internal/modules/cjs/loader.js:884:14)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at Object.<anonymous> (/mnt/config/webpack/development.js:3:21)
    at Module._compile (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32)
    at Function.Module._load (internal/modules/cjs/loader.js:884:14)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at WEBPACK_OPTIONS (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:114:13)
    at requireConfig (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:116:6)
    at /home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:123:17
    at Array.forEach (<anonymous>)
    at module.exports (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:121:15)
    at /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:71:45
    at Object.parse (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/yargs/yargs.js:567:18)
    at /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:49:8
    at Object.<anonymous> (/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:1121:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/mnt/config/webpack/environment.js',
    '/mnt/config/webpack/development.js',
    '/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js',
    '/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js',
    '/home/alejo/rails_project/node_modules/webpack/bin/webpack.js'
  ]
}

I am using webpacker 4.0.7, rails 6.0.0 and ruby 2.6.5

I want to know why am I getting this error and how do I solve it.

Thank you

Alegar answered 2/3, 2020 at 15:19 Comment(4)
Is there an @rails directory in the node_modules directory?Bookplate
Yes, it is, and there is a webpacker directory inside itAlegar
what is your project layout? why is your config directory in /mnt?Auric
I had the config folder on another location and I had a symbolic link, I solved the problem by moving the config folder to the project folderAlegar
A
17

I ran into this issue as well. For me, the issue was the node version (13.6.0). I figured this out by running yarn add @rails/webpacker, which returned the error Expected version "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7". Got "13.6.0"

To fix it:

  1. Update node to an appropriate version (see error above)
  2. Delete node_modules directory
  3. Update yarn files with yarn install --check-files
  4. Install webpacker with rails webpacker:install
  5. Confirm it worked by bin/webpack-dev-server and getting no errors
Abyssal answered 8/9, 2020 at 4:1 Comment(4)
This was a really helpful way for me to go about diagnosing my error, which was very similar but subtly different.Ideogram
for some reason rails 6 should fail if it couldnt install @rails/webpacker. instead it continues silently and complains later...Extraterritoriality
This was really helpful. I was using node version 10.22.0 which was causing this issue. Solution was to use 12.13.0. Thank you so much.Magpie
Just running rails webpacker:install and yarn install resolved my issue. Thanks!Magpie

© 2022 - 2024 — McMap. All rights reserved.