cd ./web
yarn
cd ../server
yarn
cd ../package
yarnCreate ./web/.env.local file and add the following lines:
NEXT_PUBLIC_SOCKET_URL=http://localhost:4000
Create ./server/.env file and add the following lines:
PORT=4000
- Start the server
cd ./server yarn dev - Start the web
cd ./web yarn dev - Go to
http://localhost:3000in your browser
- Create folers
Create two folders:
web,packageandserverin the root directory of the project.webfolder will contain a NextJS project.serverfolder will contain an ExpressJS project and serve as the backend.packagefolder will contain serveral shared files between thewebandserverprojects, such as types definitions, constants, etc.
- Initialize git
git init
Go to the web folder, run yarn create next-app . to create a NextJS project. Select the default options for the questions.
cd ./web
yarn create next-app .- Add a path to the
tsconfig.jsonfile{ "compilerOptions": { "paths": { "@package/*": ["../package/*"], }, } }
In the web folder, follow the below instructions to setup Prettier and ESLint.
- Install prettier and prettier plugins
yarn add -D prettier prettier-plugin-tailwindcss @trivago/prettier-plugin-sort-imports
- Install eslint and eslint plugins
yarn add -D eslint typescript @typescript-eslint/parser eslint-config-prettier @typescript-eslint/eslint-plugin
- Copy and paste the
./web/.eslintrc.jsonfile and./web.prettierrc.cjsfile from this repo to your project root
-
Setup Shadcn UI
# In the ./web folder npx shadcn-ui@latest init -
Answer the questions carefully since some of the default options are not compatible with our setup.
- Would you like to use TypeScript (recommended)?
yes - Which style would you like to use? ›
New York- I personally prefer New York style, but you can choose any style you like.
- Which color would you like to use as base color? ›
Slate- You can choose any color you like.
- Where is your global CSS file? › ›
src/app/globals.css- IMPORTANT: You must enter
src/app/globals.csshere. Otherwise, the setup will fail.
- IMPORTANT: You must enter
- Do you want to use CSS variables for colors? ›
yes - Where is your tailwind.config.js located? ›
tailwind.config.ts- IMPORTANT: We are using TypeScript, so you must enter
tailwind.config.tshere.
- IMPORTANT: We are using TypeScript, so you must enter
- Configure the import alias for components: ›
@/components - Configure the import alias for utils: ›
@/lib/utils/shadcn - Are you using React Server Components? ›
yes
- Would you like to use TypeScript (recommended)?
- Copy and paste the
./web/.env.examplefile into the./web/.env.local - Fill in the environment variables in the
./web/.env.localfile
In this tutorial, we will show how to do user authentication with NextJS and ExpressJS. Therefore, we need to redirect api routes to the ExpressJS server.
In ./web/next.config.js, add the following lines:
const nextConfig = {
...
async redirects() {
return [
{
source: "/api/:path*",
destination: `${process.env.NEXT_PUBLIC_API_URL}/:path*`,
permanent: true,
},
];
},
...
};Also, remember to add NEXT_PUBLIC_API_URL to the ./web/.env.local file. For example, if server runs at port 4000, then you have to set:
# ./web/.env.local
NEXT_PUBLIC_API_URL=http://localhost:4000
# ./server/.env
PORT=4000
yarn create next-app will automatically initialize a git repository. However, we have already initialized a git repository in the root directory of the project. Therefore, we need to remove the git repository in the web folder.
cd ./web
rm -rf .gitIn the server folder, run yarn init -y to create a NodeJS project.
cd ./server
yarn init -y- Install TypeScript and ts-node
yarn add -D ts-node typescript @types/node
- Create
tsconfig.jsonfileyarn tsc --init
- Add a path to the
tsconfig.jsonfile{ "compilerOptions": { "paths": { "@package/*": ["../package/*"], }, } }
In the server folder, follow the below instructions to setup Prettier and ESLint.
-
Install prettier and prettier plugins
yarn add -D prettier prettier-plugin-tailwindcss @trivago/prettier-plugin-sort-imports
-
Install eslint and eslint plugins
yarn add -D eslint typescript @typescript-eslint/parser eslint-config-prettier @typescript-eslint/eslint-plugin
-
Copy and paste the
./server/prettierrc.cjsfile from this repo to your project root. -
Setup ESLint In the
serverfolder, run the following command to setup ESLint.npx eslint --init
- How would you like to use ESLint? ·
To check syntax and find problems - What type of modules does your project use? ·
JavaScript modules (import/export) - Which framework does your project use? ·
None of these - Does your project use TypeScript? ·
Yes - Where does your code run? ·
Node- IMPORTANT: Press
spaceto select,ato toggle all,ito invert selection. If you only pressEnter, the setup will becomebrowserinstead ofnode.
- IMPORTANT: Press
- What format do you want your config file to be in? ·
JSON - Would you like to install them now? ·
Yes - Which package manager do you want to use? ·
yarn
- How would you like to use ESLint? ·
-
Add
lintandformatscripts topackage.json{ "scripts": { "lint": "eslint .", "format": "prettier --write ." }, }
- Install ExpressJS
yarn add express cors body-parser nodemon dotenv yarn add -D @types/express @types/cors @types/body-parser
- Add
startanddevscripts topackage.json{ "scripts": { "start":"ts-node src/index.ts", "dev": "nodemon src/index.ts", ... }, } - Create
src/index.tsfileimport express from 'express'; const app = express(); const port = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Server is listening on port ${port}`); });
- Create
./server/.envfile. Copy and paste all the content in./server/.env.examplefile into the./server/.envfile. - Fill in the environment variables in the
./server/.envfile - Create
./server/.gitignorefile and add the following linesnode_modules .env
If you completed all the steps above, you should be able to run yarn dev in the ./server folder and you will see "Hello World!" at http://localhost:<YOUR PORT NUMBER> in your browser.
In the package folder, run yarn init -y to create a NodeJS project.
cd ./package
yarn init -yIn the package folder, follow the below instructions to setup Prettier and ESLint.
- Install prettier and prettier plugins
yarn add -D prettier prettier-plugin-tailwindcss @trivago/prettier-plugin-sort-imports
- Install eslint and eslint plugins
yarn add -D eslint typescript @typescript-eslint/parser eslint-config-prettier @typescript-eslint/eslint-plugin
- Copy and paste the
./package/.eslintrc.jsonfile and./package.prettierrc.cjsfile from this repo to your project root