Need help with webpacker?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

5.1K Stars 1.4K Forks MIT License 1.4K Commits 227 Opened issues


Use Webpack to manage app-like JavaScript modules in Rails

Services available


Need anything else?

Contributors list


Ruby specs Jest specs Rubocop JS lint

node.js Gem

Webpacker makes it easy to use the JavaScript pre-processor and bundler Webpack v5 to manage application-like JavaScript in Rails. It coexists with the asset pipeline, as the primary purpose for webpack is app-like JavaScript, not images, CSS, or even JavaScript Sprinkles (that all continues to live in app/assets).

However, it is possible to use Webpacker for CSS, images and fonts assets as well, in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.

NOTE: The master branch now hosts the code for v6.x.x. Please refer to 5-x-stable branch for 5.x documentation.

Table of Contents


  • Ruby 2.4+
  • Rails 5.2+
  • Node.js 12+ || 14+
  • Yarn 1.x+ || 2.x+


  • Webpack v5
  • ES6 with babel
  • Automatic code splitting using multiple entry points
  • Asset compression, source-maps, and minification
  • CDN support
  • Rails view helpers
  • Extensible and configurable

Optional support

requires extra packages to be installed

  • Stylesheets - Sass, Less, Stylus and Css, PostCSS
  • CoffeeScript
  • TypeScript
  • React


You can either add Webpacker during setup of a new Rails 5.1+ application using new

# Available Rails 5.1+
rails new myapp --webpack

Or add it to your

# Gemfile
gem 'webpacker', '~> 6.x'

OR if you prefer to use master

gem 'webpacker', git: '' yarn add

Finally, run the following to install Webpacker:

bundle exec rails webpacker:install

OR (on rails version < 5.0)

bundle exec rake webpacker:install

Optional: To fix "unmet peer dependency" warnings,

yarn upgrade


changes, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
yarn install


Once installed, you can start writing modern ES6-flavored JavaScript apps right away:

  ├── entrypoints:
  │   # Only Webpack entry files here
  │   └── application.js
  │   └── application.css
  └── src:
  │   └── my_component.js
  └── stylesheets:
  │   └── my_styles.css
  └── images:
      └── logo.svg

You can then link the JavaScript pack in Rails views using the

helper. If you have styles imported in your pack file, you can link them by using

If you want to link a static asset for

tag, you can use the

Or use the dedicated helper:


'2x' } %>

If you want to create a favicon:


If you want to preload a static asset in your

, you can use the 

If you want to use images in your stylesheets:

.foo {
  background-image: url('../images/logo.svg')

Note, if you are using server-side rendering of JavaScript with dynamic code-spliting, as is often done with extensions to Webpacker, like React on Rails your JavaScript should create the link prefetch HTML tags that you will use, so you won't need to use to

in those circumstances.

Note: In order for your styles or static assets files to be available in your view, you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know to package up those files.


Webpacker ships with two binstubs:

. Both are thin wrappers around the standard
executables to ensure that the right configuration files and environmental variables are loaded based on your environment.

In development, Webpacker compiles on demand rather than upfront by default. This happens when you refer to any of the pack assets using the Webpacker helper methods. This means that you don't have to run any separate processes. Compilation errors are logged to the standard Rails log. However, this auto-compilation happens when a web request is made that requires an updated webpack build, not when files change. Thus, that can be painfully slow for front-end development in this default way. Instead, you should either run the

bin/webpack --watch
or run

If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run

ruby ./bin/webpack-dev-server
. Windows users will need to run these commands in a terminal separate from
bundle exec rails s
. This process will watch for changes in the
files and automatically reload the browser to match. This feature is also known as Hot Module Replacement.

HMR is only the first step to running "Fast Refresh" with React. For more information on how to configure rails/webpacker for Fast Refresh with React, see article HMR and React Hot Reloading.

# webpack dev server


./bin/webpack --watch --colors --progress

standalone build


Once you start this webpack development server, Webpacker will automatically start proxying all webpack asset requests to this server. When you stop this server, Rails will detect that it's not running and Rails will revert back to on-demand compilation if you have the

option set to true in your

You can use environment variables as options supported by webpack-dev-server in the form

. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the same environmental variables must be available to the
rails server
process. WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server

By default, the webpack dev server listens on

in development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the
when running
WEBPACKER_DEV_SERVER_HOST= ./bin/webpack-dev-server

Note: You need to allow webpack-dev-server host as an allowed origin for

if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer
with a snippet like this:
  Rails.application.config.content_security_policy do |policy|
    policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?

Note: Don't forget to prefix

when running these binstubs on Windows

Webpack Configuration

Webpacker gives you a default set of configuration files for test, development and production environments in

. You can configure each individual environment in their respective files or configure them all in the base

By default, you don't need to make any changes to

files since it's all standard production-ready configuration. However, if you do need to customize or add a new loader, this is where you would go.

Here is how you can modify webpack configuration:

You might add separate files to keep your code more organized.

// config/webpack/custom.js
module.exports = {
  resolve: {
    alias: {
      jquery: 'jquery/src/jquery',
      vue: 'vue/dist/vue.js',
      React: 'react',
      ReactDOM: 'react-dom',
      vue_resource: 'vue-resource/dist/vue-resource'


this file in your
// config/webpack/base.js
const { webpackConfig, merge } = require('@rails/webpacker')
const customConfig = require('./custom')

module.exports = merge(webpackConfig, customConfig)

If you need access to configs within Webpacker's configuration, you can import them like so:

// config/webpack/base.js
const { webpackConfig } = require('@rails/webpacker')

console.log(webpackConfig.output_path) console.log(webpackConfig.source_path)

// Or to print out your whole webpack configuration console.log(JSON.stringify(webpackConfig, undefined, 2))


Webpacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:


yarn add coffeescript coffee-loader


yarn add typescript @babel/preset-typescript

Add tsconfig.json

  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*", "app/packs/*"]
    "sourceMap": true,
    "target": "es5",
    "noEmit": true
  "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
  "compileOnSave": false

Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:

yarn add fork-ts-checker-webpack-plugin

Then modify the webpack config to use it as a plugin:

// config/webpack/base.js
const { webpackConfig, merge } = require("@rails/webpacker");
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");

module.exports = merge(webpackConfig, { plugins: [new ForkTSCheckerWebpackPlugin()], });


To enable CSS support in your application, add following packages:

yarn add css-loader mini-css-extract-plugin css-minimizer-webpack-plugin

optionally, add the css extension to webpack config for easy resolution.

// config/webpack/base.js
const { webpackConfig, merge } = require('@rails/webpacker')
const customConfig = {
  resolve: {
    extensions: ['.css']

module.exports = merge(webpackConfig, customConfig)

To enable postcss, sass or less support, add css support first and then add the relevant pre-processors:


yarn add postcss-loader


yarn add sass sass-loader


yarn add less less-loader


yarn add stylus stylus-loader


React is supported and you just need to add relevant packages,

yarn add react react-dom @babel/preset-react

if you are using typescript, update your

  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "noEmit": true
  "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
  "compileOnSave": false

For more information on React props hydration and Server-Side Rendering (SSR), see the article Rails/Webpacker React Integration Options in the ShakaCode/reactonrails repo.

Other frameworks

Please follow webpack integration guide for relevant framework or library,

  1. Svelte
  2. Angular
  3. Vue

For example to add Vue support: ```js // config/webpack/rules/vue.js const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = { module: { rules: [ { test: /.vue$/, loader: 'vue-loader' } ] }, plugins: [new VueLoaderPlugin()] } ```

// config/webpack/base.js
const { webpackConfig, merge } = require('@rails/webpacker')
const vueConfig = require('./rules/vue')

module.exports = merge(webpackConfig, vueConfig)

Custom Rails environments

Out of the box Webpacker ships with - development, test and production environments in

however, in most production apps extra environments are needed as part of deployment workflow. Webpacker supports this out of the box from version 3.4.0+ onwards.

You can choose to define additional environment configurations in webpacker.yml,

  <<: production depends on precompilation of packs prior to booting for performance. compile: false cache manifest.json performance cache_manifest: true compile staging a separate directory public_output_path: packs-staging>

or, Webpacker will use production environment as a fallback environment for loading configurations. Please note,

can either be set to
. This means you don't need to create additional environment files inside
and instead use webpacker.yml to load different configurations using

For example, the below command will compile assets in production mode but will use staging configurations from

if available or use fallback production environment configuration:
RAILS_ENV=staging bundle exec rails assets:precompile

And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration

RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile

Please note, binstubs compiles in development mode however rake tasks compiles in production mode.

# Compiles in development mode unless NODE_ENV is specified, per the binstub source

Compiles in production mode by default unless NODE_ENV is specified, per lib/tasks/webpacker/compile.rake

bundle exec rails assets:precompile bundle exec rails webpacker:compile


You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:

# check your Gemfile for version restrictions
bundle update webpacker

overwrite your changes to the default install files and revert any unwanted changes from the install

rails webpacker:install

yarn upgrade @rails/webpacker --latest yarn upgrade webpack-dev-server --latest

Or to install the latest release (including pre-releases)

yarn add @rails/[email protected]

Also, consult the CHANGELOG for additional upgrade links.


By default, Webpacker ships with simple conventions for where the JavaScript app files and compiled webpack bundles will go in your Rails app. All these options are configurable from


The configuration for what webpack is supposed to compile by default rests on the convention that every file in

(default) or whatever path you set for
in the
configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside
directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "packs" directory and keep everything else under

Suppose you want to change the source directory from

and output to
. This is how you would do it:
# config/webpacker.yml
source_path: frontend # packs are in frontend/packs
public_output_path: assets/packs # outputs to => public/assets/packs

Similarly you can also control and configure

settings from
# config/webpacker.yml
    host: localhost
    port: 3035

If you have

turned to true, then the
generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the
will create the appropriate HTML tags.

Additional paths

If you are adding Webpacker to an existing app that has most of the assets inside

or inside an engine, and you want to share that with webpack modules, you can use the
option available in
. This lets you add additional paths that webpack should look up when resolving modules:
additional_paths: ['app/assets', 'vendor/assets']

You can then import these items inside your modules like so:

// Note it's relative to parent directory i.e. app/assets
import 'stylesheets/main'
import 'images/rails.png'

Note: Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules


Webpacker hooks up a new

task to
, which gets run whenever you run
. If you are not using Sprockets,
is automatically aliased to
. Similar to sprockets both rake tasks will compile packs in production mode but will use
to load configuration from
(if available).

When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use

yarn install --frozen-lockfile
to install NPM packages on the remote host to ensure that the installed packages match the

If you are using a CDN setup, webpacker will use the configured asset host value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the

environment variable.


See the doc page for Troubleshooting.


Code Helpers

We encourage you to contribute to Webpacker! See CONTRIBUTING for guidelines about how to proceed.


Webpacker is released under the MIT License.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.