'Fast Refresh with Next.js development mode in VS Code Remote Container/devcontainer
I can't get Next.js' Fast Refresh feature to work with a VS Code Remote Container. I can run npm run dev and see the app running on localhost on my machine, so the container works fine - only the Fast Refresh has no effect at all.
Next.js version: v11.0.1
I tried this both with Windows 10 and Ubuntu 20.04 (on WSL 2).
I already tried to use a custom webpack middleware in the next.config.js like so (see https://github.com/vercel/next.js/issues/2179#issuecomment-316568536):
module.exports = {
webpackDevMiddleware: (config) => {
// Solve compiling problem via vagrant
config.watchOptions = {
poll: 1000, // Check for changes every second
aggregateTimeout: 300, // delay before rebuilding
};
return config;
},
};
...which will trigger a recompile on code changes, but the browser does not update.
Also, the requests to "HMR" are failing:

How to reproduce:
- Install the Remote Containers extension
- Open any new folder
- Open the command palette and type/select "Remote-Containers: Rebuild and Reopen in Container"
- Type/select "Node.js"
- Type/select version "16" and wait for the container to start
- Go to the
.devcontainerfolder and open thedevcontainer.json - Edit the config by adding
"forwardPorts": [3002],to make the app available on your host and rebuild the container (via VS Code's command palette) - From the terminal, install Next.js, e.g.:
npx create-next-app --use-npm --example with-typescript-eslint-jest my-app - Move all the files from
my-appto your VS Code project root folder. This has to be done becausecreate-next-appdoes not work installing in the project root folder via., because there's already the.devcontainerfolder. - Optional: Create a
next.config.jsand add the snippet for the Webpack dev middleware as seen above - Edit the
package.jsonscript to use a specific port:"dev": "next dev -p 3002",(or, if you use WSL 2:next dev -p 3002 -H ::) - From the terminal, start the app
npm run dev - Open the browser on
http://localhost:3002 - The app is showing. Make changes in the code -> even a recompiled app will not show the changes in the browser. A reload of the page in the browser will show the changes though.
With Create React App, there's an advanced configuration without ejecting (called CHOKIDAR_USEPOLLING), which makes their Fast Refresh work with Remote Containers.
Earlier I created a feature request, but maybe someone already managed to make this work without huge changes in the configuration/setup?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
