Github url


by jaredpalmer

jaredpalmer /razzle

✨ Create server-rendered universal JavaScript applications with no configuration

9.3K Stars 810 Forks Last release: 13 days ago (v3.1.6) MIT License 794 Commits 81 Releases

Available items

No Items, yet!



CircleCI Razzle-status npm version Known Vulnerabilities Greenkeeper badge Join the community on Spectrum

Universal JavaScript applications are tough to setup. Either you buy into a framework like Next.js or react-server, fork a boilerplate, or set things up yourself. Aiming to fill this void, Razzle is a tool that abstracts all complex configuration needed for SSR into a single dependency--giving you the awesome developer experience of create-react-app, but then leaving the rest of your app's architectural decisions about frameworks, routing, and data fetching up to you. With this approach, Razzle not only works with React, but also Reason, Elm, Vue, Angular, and most importantly......whatever comes next.

Razzle comes with the "battery-pack included":

  • :fire: Universal Hot Module Replacement, so both the client and server update whenever you make edits. No annoying restarts necessary
  • Comes with your favorite ES6 JavaScript goodies (through
  • Comes with the same CSS setup as create-react-app
  • Works with React, Preact, Elm, Reason-React, Inferno, and Rax as well as Angular and Vue if that's your thing
  • Escape hatches for customization via
  • Jest test runner setup with sensible defaults via
    razzle test
  • :rocket: SPA mode, build client side apps with razzle

Quick Start

npx create-razzle-app my-app cd my-app npm start

Then open http://localhost:3000/ to see your app. Your console should look like this:

Razzle Development Mode

That's it. You don't need to worry about setting up multiple webpack configs or other build tools. Just start editing


and go!

Below is a list of commands you will probably find useful.

npm start


yarn start

Runs the project in development mode.
You can view your application at


The page will reload if you make edits.

npm run build


yarn build

Builds the app for production to the build folder.

The build is minified and the filenames include the hashes. Your app is ready to be deployed!

npm run start:prod


yarn start:prod

Runs the compiled app in production.

You can again view your application at


npm test


yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.

npm start -- --inspect=[host:port]


yarn start -- --inspect=[host:port]

To debug the node server, you can use

razzle start --inspect

. This will start the node server and enable the inspector agent. The


is optional and defaults to


. For more information, see this.

npm start -- --inspect-brk=[host:port]


yarn start -- --inspect-brk=[host:port]

This is the same as --inspect, but will also break before user code starts. (to give a debugger time to attach before early code runs) For more information, see this.


If your application is running, and you need to manually restart your server, you do not need to completely kill and rebundle your application. Instead you can just type


and press enter in terminal.

Razzle Hot Restart

In addition to universal/isomorphic appplications, Razzle can build single page (or client-only) applications. To do this, you can remove






file inside public folder at the end pass


to your


's scripts like so:

"scripts": { - "start": "razzle start", + "start": "razzle start --type=spa", - "build": "razzle build", + "build": "razzle build --type=spa", "test": "razzle test --env=jsdom", - "start:prod": "NODE\_ENV=production node build/server.js" + "start:prod": "serve -s build/public" }



As of Razzle 2.0, you can add your plugins to modify your setup.

Using Plugins

You can use Razzle plugins by installing in your project and adding them to your


. See the of the specific plugin, but generally speaking, the flow is something like...

yarn add razzle-plugin-xxxx
//./razzle.config.js module.exports = { plugins: ['xxxx'], };

Writing Plugins

Plugins are simply functions that modify and return Razzle's webpack config.

'use strict'; module.exports = function myRazzlePlugin(config, env, webpack, options) { const { target, dev } = env; if (target === 'web') { // client only } if (target === 'node') { // server only } if (dev) { // dev only } else { // prod only } // Do some stuff... return webpackConfig; };

Customizing Babel Config

Razzle comes with most of ES6 stuff you need. However, if you want to add your own babel transformations, just add a


file to the root of your project.

{ "presets": ["razzle/babel", // NEEDED "stage-0"], "plugins": [// additional plugins] }

A word of advice: the


file will replace the internal razzle babelrc template. You must include at the very minimum the default razzle/babel preset.

Extending Webpack

You can also extend the underlying webpack config. Create a file called


in your project's root.

// razzle.config.js module.exports = { modify: (config, { target, dev }, webpack) =\> { // do something to config return config; }, };

A word of advice:


is an escape hatch. However, since it's just JavaScript, you can and should publish your


function to npm to make it reusable across your projects. For example, imagine you added some custom webpack loaders and published it as a package to npm as


. You could then write your


like so:

// razzle.config.js const modify = require('my-razzle-modifictions'); module.exports = { modify }

Last but not least, if you find yourself needing a more customized setup, Razzle is very forkable. There is one webpack configuration factory that is 300 lines of code, and 4 scripts (






, and


). The paths setup is shamelessly taken from create-react-app, and the rest of the code related to logging.

CSS Modules

Razzle supports CSS Modules using Webpack's css-loader. Simply import your CSS file with the extension


and Razzle will process the file using



import React from 'react'; import styles from './style.module.css'; const Component = () =\> 

; export default Component;


Polyfills for IE 9, IE 10, and IE 11 are no longer included by default (but you can opt in!) We have dropped default support for Internet Explorer 9, 10, and 11. If you still need to support these browsers, follow the instructions below.

First, install



npm install react-app-polyfill


yarn add react-app-polyfill

Next, place one of the following lines at the very top of

import 'react-app-polyfill/ie9'; // For IE 9-11 support import 'react-app-polyfill/ie11'; // For IE 11 support

Environment Variables

Build-time Variables

The following environment variables are embedded during the build time.

  • process.env.RAZZLE\_PUBLIC\_DIR
    : Absolute path to the public directory in the server's filesystem.
  • process.env.RAZZLE\_CHUNKS\_MANIFEST
    : Path to a file containing compiled chunk outputs
  • process.env.RAZZLE\_ASSETS\_MANIFEST
    : Path to a file containing compiled asset outputs
  • process.env.REACT\_BUNDLE\_PATH
    : Relative path to where React will be bundled during development. Unless you are modifying the output path of your webpack config, you can safely ignore this. This path is used by
    and webpack to power up the fancy runtime error iframe. For example, if you are using common chunks and an extra entry to create a vendor bundle with stuff like react, react-dom, react-router, etc. called
    , and you've changed webpack's output to
    in development, you'd want to set this environment variable to
    . If you do not make this change, nothing bad will happen, you will simply not get the cool error overlay when there are runtime errors. You'll just see them in the console. Note: This does not impact production bundling.
  • process.env.VERBOSE
    : default is false, setting this to true will not clear the console when you make edits in development (useful for debugging).
  • process.env.PORT
    : The
    build listens on this port for all NODE_ENVs. default is
  • process.env.HOST
    : The IP address that the server will bind to. default is ```











 for isomorphic/universal applications or 


 for single page applications. The default is 


. This is set by CLI arguments.


: either 






: Only used in 

razzle build

. You can alter the 


 of the client assets (bundle, css, and images). This is useful if you plan to serve your assets from a CDN. Make sure to _include_ a trailing slash (e.g. 


). If you are using React and altering the public path, make sure to also [include the 


 attribute]( on your 
 tag in 




: The 




 has a different 




. Default is 

http://${process.env.HOST}:${process.env.PORT + 1}/

You can create your own custom environment variables that will be inlined during the build. They must start with


. Any other variables except the ones listed above will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

These environment variables will be defined for you on


. For example, having an environment variable named 


 will be exposed in your JS as 


### Runtime Variables

Using the dotenv package, or by defining variables in your shell (see below), you can get access to runtime environment variables. This is useful for services like Heroku which dynamically set


 for example. Be careful when referencing runtime variables in isomorphic code as they will be 


 in the browser, but defined when running in Node. This can lead to weird behavior. If you need to make runtime variables available to the browser, it is up to you to deliver them. You can stringify them and place them on 



// config.js export const runtimeConfig = typeof window !== 'undefined' ? { // client myThing: window.env.myThing, anotherThing: window.env.anotherThing, } : { // server myThing: process.env.MY_THING, anotherThing: process.env.ANOTHER_THING, };

Now we set




 when we go to render the HTML.

import App from './App'; import React from 'react'; import express from 'express'; import { renderToString } from 'react-dom/server'; import serialize from 'serialize-javascript'; // Safer stringify, prevents XSS attacks import { runtimeConfig } from './config'; const chunks = require(process.env.RAZZLE_CHUNKS_MANIFEST); const server = express(); server .disable('x-powered-by') .use(express.static(process.env.RAZZLE_PUBLIC_DIR)) .get('/*', (req, res) => { const markup = renderToString(); res.send( // prettier-ignore <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8"><title>Welcome to Razzle</title><meta name="viewport" content="width=device-width, initial-scale=1"> ${ =\>`)} ${markup}

${ =\> ``)} ` ); }); export default server;

### Adding Temporary Environment Variables In Your Shell


#### Windows (cmd.exe)

set RAZZLE_SECRET_CODE=abcdef&&npm start

(Note: the lack of whitespace is intentional.)

#### Linux, macOS (Bash)

RAZZLE_SECRET_CODE=abcdef npm start

### Adding Environment Variables In 




### Expanding Environment Variables In 


Expand variables already on your machine for use in your



For example, to get the environment variable



RAZZLE_VERSION=$npm_package_version # also works: # RAZZLE_VERSION=${npm_package_version}

Or expand variables local to the current



#### What other 


 files are can be used?



: Default.


: Local overrides. **This file is loaded for all environments except test.**






: Environment-specific settings.






: Local overrides of environment-specific settings.



npm start










npm run build










npm test









 is missing)

Please refer to the [dotenv documentation]( for more details.

> Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI]( or [Heroku](

## How Razzle works (the secret sauce)

**TL;DR**: 2 configs, 2 ports, 2 webpack instances, both watching and hot reloading the same filesystem, in parallel during development and a little



In development mode (

razzle start

), Razzle bundles both your client and server code using two different webpack instances running with Hot Module Replacement in parallel. While your server is bundled and run on whatever port you specify in 




 is the default), the client bundle (i.e. entry point at 


) is served via 


 on a different port (


 by default) with its 


 explicitly set to 


 (and not 


 like many other setups do). Then the server's html template just points to the absolute url of the client JS: 


``` . Since both webpack instances watch the same files, whenever you make edits, they hot reload at exactly the same time. Best of all, because they use the same code, the same webpack loaders, and the same babel transformations, you never run into a React checksum mismatch error.



MIT License


Thanks goes to these wonderful people (emoji key):

Jared Palmer

💬 💻 🎨 📖 💡 🤔 👀 ⚠️ 🔧 |
Jari Zwarts

💬 💻 🤔 🔌 👀 |
Dan Abramov

💻 🤔 |
Eric Clemmons

💻 🤔 |
Zino Hofmann

💡 |
Lucas Terra

💻 💡 🔌 |
Ray Andrew

💻 💡 🔌 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
Erik Engi

📖 📝 💡 🐛 |

