3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Next.js + TypeScript with ESLint and Prettier in VSCode で わけも わからず 環境構築 する

Last updated at Posted at 2021-03-02

追記(2021.9.26)

もうこれでいい

yarn create next-app --typescript

以下、もういらない文章です。
いや、VSCodeのところはあってもいい。

1. はじめに

なんかいい感じのWebアプリ作りたいと思っていろいろ調べたら
Next.jsが良いって書いてあったから手を出してみたら
TypeScriptを使えるようにする設定が地味面倒だったので
混乱状態になってもコピペすれば環境構築できるように残しておきます。

参考ページ

内容としては①を90割パクって
TypeScriptにするとこだけ
②を10割ほどパクりました。

https://zenn.dev/66ed3gs/articles/99aa613a86f21f
https://www.getshifter.io/ja/next-js/

前提となる環境

Windows10、Powershell、node v14.15.4
nodeは入っている前提です、ゴメンネ

2. 環境構築

アプリ名決めよう

端末を開いてダブルクォーテーションの中に
自分が作るアプリ名を決めて入力しましょう。

$app_name = "super-ecchi-app"

コマンド叩こう

こっから先はまるごとコピペしてエンターでええです。
細かい内容は参考ページ見たりググってください。

# 上で決めたアプリ名のディレクトリを作って移動
mkdir $app_name; cd $app_name
# 現在のディレクトリにNext.js + TypeScriptでいい感じにアプリ雛形を作成
npx create-next-app . --example "https://github.com/wpkyoto/nextjs-starter-typescript/tree/main"
# srcディレクトリを作成してpages, styleディレクトリをその中に突っ込む
mkdir src; mv pages src; mv styles src
# いろいろ入れる(npm i => yarn add でも可)
npm i -D typescript @types/react @types/react-dom @types/node
npm i -D eslint prettier eslint-config-prettier
npm i -D eslint-plugin-react eslint-plugin-react-hooks
npm i -D @typescript-eslint/parser @typescript-eslint/eslint-plugin

VSCodeの拡張機能いれよう

VSCodeで次の拡張機能を入れてください。
VSCode開いて、Ctrl + Shift + X でサイドメニューの
拡張機能が開きますのでそこで検索して入れてください。

  • ESLint

image.png

  • Prettier

image.png

設定ファイル足そう

コードの見た目整えたり書いてる途中に文法チェックとか
するために3つの設定ファイルを追加します。

  • .eslintrc.json
  • .prettierrc.json
  • .vscode/settings.json

上2つはアプリを作ったディレクトリと同じ場所、
下のsettings.jsonは".vscode"というディレクトリを
作成してその下に置いてください。

ファイルはとりあえず以下をコピペしてください。
設定弄りたい人は有識者に聞くか自分でググってください。

..eslintrc.json
{
  "env": {
    "es6": true,
    "node": true,
    "browser": true,
    "commonjs": true
  },
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly",
    "React": "writable"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "plugins": ["react-hooks", "react", "@typescript-eslint", "prettier"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "prettier",
    "prettier/react",
    "prettier/@typescript-eslint"
  ],
  "rules": {
    "react/prop-types": "off",
    "prettier/prettier": "error",
    "react/react-in-jsx-scope": "off"
  },
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": "off",
        "@typescript-eslint/explicit-function-return-type": "off"
      }
    }
  ]
}
..prettier.json
{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 100,
  "tabWidth": 2
}
..vscode/settings.json
{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

VSCodeのフォルダの画面がこんな感じ↓になってれば大丈夫です。
image.png

動かしてみよう

ここまで出来たらアプリを作成したディレクトリで
端末を開いて次のコマンドを打ってください。

npm run dev

ブラウザで下のURLアクセスして
なんかそれっぽい画面が見えていたらおk
http://localhost:3000

同じwi-fi繋いでればスマホのブラウザからでも見えるよ。

3. おわりに

scssも使いてぇよなぁ~~~~~

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?