in Everything

Debugging silently failing compilation aka Webpacker can’t find application.js in public/packs/manifest.json

Webpacker can pretend everything went smooth with the compilation… until one loads a page with javascript_pack_tag helper.

I was deploying a new version of one application that included latest pre-release of Webpacker to production and it failed with:

Webpacker::Manifest::MissingEntryError:

Webpacker can't find application.js in .../app/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}

After investigating the .../app/public/packs path on the server I found out there no packs generated whatsoever! I checked that locally I am able to generate the assets just fine both with:

$ RAILS_ENV=production rails assets:precompile

or:

$ RAILS_ENV=production rails webpacker:compile

However on the server those commands did nothing. No errors. They even happily printed:

Compiling…
Compiled all packs in .../app/public/packs

After that I went on a typical programmer journey to find out what might be the issue. I went to google. Webpacker can’t find application.js was indeed a discovered error and yielded various Webpacker issues. Unfortunately nothing seemed like a fix for my broken deploy and I went to read the source code. I looked for a place where Webpacker happily prints out that compilation was a success.

This is the method:

def run_webpack
  logger.info "Compiling…"

  stdout, sterr , status = Open3.capture3(webpack_env, "#{RbConfig.ruby} #{@webpacker.root_path}/bin/webpack")

  if sterr == "" && status.success?
    logger.info "Compiled all packs in #{config.public_output_path}"
    logger.info stdout if config.webpack_compile_output?
  else
    logger.error "Compilation failed:\n#{sterr}\n#{stdout}"
  end

  status.success?
end

As you can see it just runs webpack from the correct path and checks for the command standard error output and exit code.

So what if I run bin/webpack on the server?

$ bin/webpack
warning package.json: No license field                                                                                                                                                                            
One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
 - webpack-cli (https://github.com/webpack/webpack-cli)
   The original webpack full-featured CLI.
We will use "yarn" to install the CLI via "yarn add -D".
Do you want to install 'webpack-cli' (yes/no): yes
Installing 'webpack-cli' (running 'yarn add -D webpack-cli')...

I am missing Webpack CLI which I of course have as a development dependency locally so it never installed on the production server during build (and gave me a headache).

Resolution: don’t put webpack-cli package only as a dev dependency if you need to do the compilation on the server.

If you by any chance come here because of Webpack error and this is not your error make sure to check the troubleshooting guide.

Write a Comment

Comment