An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
Next.js TypeScript Tailwind CSS project setup
1. Set up the Next.js project
First, create-next-appuse the command to create a codebase from the Next.js template.
npx create-next-app nextjs-ts-tailwind-example --use-npm --example "https://github.com/vercel/next-learn-starter/tree/master/basics-final"
When the command is completed, the code of Next.js is generated, so move the directory and check the operation.
cd nextjs-ts-tailwind-examplenpm run dev
2. Set up typescript
create config files for typescript
touch tsconfig.json next-env.d.ts
Install the packages needed to run TypeScript.
npm install --save-dev typescript @types/react @types/node
add to the Typescript config file
//tsconfig.json { "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": false, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve" }, "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx" ], "exclude": [ "node_modules" ]}
add to the next-env.d.ts config file
/// <reference types="next" />/// <reference types="next/types/global" />
Next, rewrite various js files to ts files by referring to the following repository.
mv components/date.js components/date.tsxmv components/layout.js components/layout.tsxmv lib/posts.js lib/posts.tsmv pages/_app.js pages/_app.tsxmv pages/index.js pages/index.tsxmv 'pages/posts/[id].js' 'pages/posts/[id].tsx'mv pages/api/hello.js pages/api/hello.ts
After rewriting, check the operation.
npm run dev
Check the operation with a browser and it is OK if it works without error.
3. Set up Tailwind css
npm install tailwindcss@latest postcss@latest autoprefixer@latest
generate a Tailwind CSS configuration file.
npx tailwindcss init -p
Then, tailwind.config.js the postcss.config.js two files will be generated.
//tailwind.config.jsmodule.exports = { purge: [], //remove this line purge: ['./components/**/*.tsx', './pages/**/*.tsx','./public/**/*.html'], //add this line darkMode: false, // or 'media' or 'class' theme: { extend: {}, }, variants: { extend: {}, }, plugins: [],}
//postcss.config.jsmodule.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, },}
Rewrite to read the CSS generated by Tailwind CSS styles/global.css.
//styles/global.css@tailwind base;@tailwind components;@tailwind utilities;
Now you can use Tailwind CSS!
Original Link: https://dev.to/waldo/next-js-typescript-tailwind-css-project-setup-4kcj
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To