This article demonstrates how to migrate a Next.js application to the Node.js Selector in cPanel. Next.js is a web development framework based on React and Node.js.
The following procedure demonstrates how to create a basic Next.js project and prepare it for migration.
To create a Next.js project and prepare it for migration, follow these steps:
npx create-next-app@latest
Copy the following text and then paste it into the server.js file:
const { createServer } = require('http') const { parse } = require('url') const next = require('next') const dev = process.env.NODE_ENV !== 'production' const hostname = 'localhost' const port = 3000 const app = next({ dev, hostname, port }) const handle = app.getRequestHandler() app.prepare().then(() => { createServer(async (req, res) => { try { // Be sure to pass `true` as the second argument to `url.parse`. // This tells it to parse the query portion of the URL. const parsedUrl = parse(req.url, true) const { pathname, query } = parsedUrl if (pathname === '/a') { await app.render(req, res, '/a', query) } else if (pathname === '/b') { await app.render(req, res, '/b', query) } else { await handle(req, res, parsedUrl) } } catch (err) { console.error('Error occurred handling', req.url, err) res.statusCode = 500 res.end('internal server error') } }) .once('error', (err) => { console.error(err) process.exit(1) }) .listen(port, () => { console.log(`> Ready on http://${hostname}:${port}`) }) })
Save your changes to the server.js file.
Open the package.json file in your preferred text editor. Locate the “scripts” section and replace the existing “start” line with the following line:
"start": "NODE_ENV=production node server.js",
At the command prompt, change to the nextapp directory, and then type the following command:
npm run build
In the nextapp directory, type the following command:
zip -r ../nextapp.zip . --exclude ".git/*" .gitignore "node_modules/*" README.md
You are now ready to create a Node.js application on your hosting account in cPanel, and migrate the Next.js project to it.
To migrate a Next.js project to cPanel's Node.js Selector, follow these steps:
Create a Node.js application to run the Next.js application:
On the Node.js selector page, click
In the Node.js version list box, select the Node.js version.
Click CREATE. cPanel creates the application:
Click Run NPM Install.
In your web browser, go to example.com, where example.com represents your domain name. The Next.js application should appear:
To view the official Next.js documentation, please visit https://nextjs.org/docs.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.