--- title: Using Next.js with Expo for Web sidebar_title: Using Next.js description: A guide for integrating Next.js with Expo for the web. --- import { Collapsible } from '~/ui/components/Collapsible'; import { Terminal } from '~/ui/components/Snippet'; > **Warning:** Using Next.js is not an official part of Expo's universal app development workflow. [Next.js][nextjs] is a React framework that provides simple page-based routing as well as server-side rendering. To use Next.js with the Expo SDK, we recommend using [`@expo/next-adapter`][next-adapter] library to handle the configuration. Using Expo with Next.js means you can share some of your existing components and APIs across your mobile and web app. Next.js has its own CLI that you'll need to use when developing for the web platform, so **you'll need to start your web projects with the Next.js CLI and not with `npx expo start`.** > Next.js can only be used with Expo for web, this doesn't provide Server-Side Rendering (SSR) for native apps. ## Setup To get started, create a new project with [the template](https://github.com/expo/examples/tree/master/with-nextjs): - **Native**: `npx expo start` -- start the Expo project - **Web**: `npx next dev` -- start the Next.js project ## Manual setup ### Install dependencies Ensure you have `expo`, `next`, `@expo/next-adapter` installed in your project: ### Transpilation Configure Next.js to transform language features: Next.js with swc. (Recommended)}> When using Next.js with SWC, you can configure the **babel.config.js** to only account for native. ```js babel.config.js module.exports = function (api) { api.cache(true); return { presets: ['babel-preset-expo'], }; }; ``` You will also have to [force Next.js to use SWC](https://nextjs.org/docs/messages/swc-disabled) by adding the following to your **next.config.js**: ```js next.config.js module.exports = { experimental: { forceSwcTransforms: true, }, }; ``` Next.js with Babel. (Not recommended)}> Adjust your **babel.config.js** to conditionally add `next/babel` when bundling with webpack for web. ```js babel.config.js module.exports = function (api) { // Detect web usage (this may change in the future if Next.js changes the loader) const isWeb = api.caller( caller => caller && (caller.name === 'babel-loader' || caller.name === 'next-babel-turbo-loader') ); return { presets: [ // Only use next in the browser, it'll break your native project isWeb && require('next/babel'), 'babel-preset-expo', ].filter(Boolean), }; }; ``` ### Next.js configuration Add the following to your **next.config.js**: ```js next.config.js const { withExpo } = require('@expo/next-adapter'); module.exports = withExpo({ // transpilePackages is a Next.js +13.1 feature. // older versions can use next-transpile-modules transpilePackages: [ 'react-native', 'expo', // Add more React Native / Expo packages here... ], }); ``` The fully qualified Next.js config may look like: ```js next.config.js const { withExpo } = require('@expo/next-adapter'); /** @type {import('next').NextConfig} */ const nextConfig = withExpo({ reactStrictMode: true, swcMinify: true, transpilePackages: [ 'react-native', 'expo', // Add more React Native / Expo packages here... ], experimental: { forceSwcTransforms: true, }, }); module.exports = nextConfig; ``` ### React Native Web styling The package `react-native-web` builds on the assumption of reset CSS styles. Here's how you reset styles in Next.js using the **pages** directory. ```js pages/_document.js import { Children } from 'react'; import Document, { Html, Head, Main, NextScript } from 'next/document'; import { AppRegistry } from 'react-native'; // Follows the setup for react-native-web: // https://necolas.github.io/react-native-web/docs/setup/#root-element // Plus additional React Native scroll and text parity styles for various // browsers. // Force Next-generated DOM elements to fill their parent's height const style = ` html, body, #__next { -webkit-overflow-scrolling: touch; } #__next { display: flex; flex-direction: column; height: 100%; } html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; } body { /* Allows you to scroll below the viewport; default value is visible */ overflow-y: auto; overscroll-behavior-y: none; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; -ms-overflow-style: scrollbar; } `; export default class MyDocument extends Document { static async getInitialProps({ renderPage }) { AppRegistry.registerComponent('main', () => Main); const { getStyleElement } = AppRegistry.getApplication('main'); const page = await renderPage(); const styles = [