Back to all posts
May 3, 20263 min readAI-generated

Setting up Next.js 15 with TypeScript from Scratch

1. What you'll need

setuptutorialguide

TechSilo

Curated by human, written by AI

1. **What you'll need**

To get started with Next.js 15 and TypeScript, you'll need Node.js 18.15.0 or later, and npm 9.5.0 or later. You'll also need a code editor or IDE of your choice, such as Visual Studio Code.

2. **Step 1: Create a new Next.js project**

Open your terminal and run the following command to create a new Next.js project with TypeScript: npx create-next-app@latest my-next-app --ts. This will create a new directory called my-next-app with the basic file structure for a Next.js project with TypeScript.

3. **Step 2: Install required dependencies**

Navigate into the new project directory: cd my-next-app. Then, install the required dependencies: npm install. This step is crucial because it installs the dependencies specified in the package.json file, which are necessary for the project to run.

4. **Step 3: Understand the project structure**

Take a look at the project structure. You should see the following files and directories: pages, public, styles, components, next.config.js, tsconfig.json, and package.json. The tsconfig.json file is used to configure the TypeScript compiler.

5. **Step 4: Configure TypeScript**

Open the tsconfig.json file and make sure it looks like this:

json
{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "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"]
}

This configuration tells TypeScript to compile the code in the pages and components directories.

6. **Step 5: Start the development server**

Start the development server by running: npm run dev. This will start the server and make your application available at http://localhost:3000.

7. **Verify it works**

Open your web browser and navigate to http://localhost:3000. You should see the default Next.js page. This verifies that the setup was successful.

8. **Common errors**

If you encounter the error TypeError: Cannot read properties of undefined (reading 'default'), it's likely because you're trying to import a module that doesn't have a default export. To fix this, make sure to import the module correctly, for example: import { moduleName } from 'module-name'.

If you encounter the error TS2307: Cannot find module '@next/types', it's likely because you're using an outdated version of @types/next. To fix this, update @types/next to the latest version: npm install --save-dev @types/next@latest.

9. **Next steps**

Now that you have Next.js 15 with TypeScript set up, you can start building your application. You can create new pages in the pages directory, and components in the components directory. You can also customize the next.config.js file to configure the Next.js server. For more information, check out the [Next.js documentation](https://nextjs.org/docs).

Enjoyed this?

This post was AI-generated and human-curated. Want more like this?