Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

Next.js14の開発環境構築手順 step02 eslintとprettierを設定する

はじめに

Next.js13の開発環境構築手順 step02 をやっていこうと思います。

Next.jsのベーシックな設定を確認する

Next.jsのベーシックなESLintの設定を確認するために、まずは以下のドキュメントを読みました。

eslint-config-next の確認をする

Next.js アプリケーションを create したときに生成された package.json の dependencies に初めから eslint-config-next があったので、どのようなpluginが既に含まれているか、 node_modules/eslint-config-next/index.js を確認しました。

.eslintrc.js に root: true を設定する

以下の通り .eslintrc.js を編集します。

module.exports = {
  extends: [
    'next/core-web-vitals',
  ],
  root: true,
};

以下の eslint v8 のページを参考にしています。

eslintrc は eslint v9 以降では非推奨となっており、 root プロパティも無くなっているようです。

eslint:recommended の設定を追加する

ベースとして eslint:recommended の設定を適用します。

以下の通り .eslintrc.js を編集します。

module.exports = {
  extends: ["eslint:recommended", "next/core-web-vitals"],
  root: true,
};

@typescript-eslint/eslint-plugin をインストールして設定する

TypeScriptで書いていくので @typescript-eslint の recommended な設定を適用します。

パッケージをインストールします。

npm install --save-dev @typescript-eslint/eslint-plugin

以下の通り .eslintrc.js を編集します。

module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'next/core-web-vitals',
  ],
  plugins: ['@typescript-eslint'],
  parserOptions: {
    project: true,
    tsconfigRootDir: __dirname,
  },
  root: true,
};

記述した内容について、基本的なことを一応補足しておきます。

pluginsに @typescript-eslint を追加することで、今インストールした @typescript-eslint/eslint-plugin に定義されたルールが使用可能になります。

ただ、これだけでは使用可能になるだけで、実際にルールを適用しているわけではないので、extends に plugin:@typescript-eslint/recommendedplugin:@typescript-eslint/recommended-requiring-type-checking を追記することで、ルールが適用されます。

どのルールを適用するか(extendsするか)については、以下のページを参考にして決めました。

上記した参考ドキュメント ( https://typescript-eslint.io/linting/configs/ ) に

We recommend most projects use recommended-requiring-type-checking (which requires typed linting).

ほとんどのプロジェクトでは、required-requiring-type-checking (型付きリンティングが必要) を使用することをお勧めします。

と書いてあったので plugin:@typescript-eslint/recommended-requiring-type-checking も設定しています。

また、( https://typescript-eslint.io/architecture/parser/#project )には

This option allows you to provide a path to your project's tsconfig.json. This setting is required if you want to use rules which require type information.

このオプションを使用すると、プロジェクトの tsconfig.json へのパスを指定できます。 タイプ情報を必要とするルールを使用する場合は、この設定が必要です。

と記載されているため parserOptions の設定もしています。

prettier と eslint-config-prettier をインストールして設定する

インストール

以下のコマンドを実行しました。

npm install --save-dev prettier eslint-config-prettier

.eslintrc.jsの設定をする

以下の通り .eslintrc.js を編集します。

module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'next/core-web-vitals',
    'prettier',
  ],
  plugins: ['@typescript-eslint'],
  parserOptions: {
    project: true,
    tsconfigRootDir: __dirname,
  },
  root: true,
};

prettierは以下の公式ドキュメントに倣って extends の最後に記述しています。

.prettierrcを作成する

ファイルを作成します。

vim .prettierrc

以下の内容を記述します。

{
  "printWidth": 120,
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2
}

@types/eslint-config-prettier を追加する

typesyncを実行します。

npx typesync

@types/eslint-config-prettier をインストールします。

npm install

eslint-plugin-importを設定する

node_modules/eslint-config-next/index.js を先ほど確認したときに、 eslint-plugin-import が含まれていることは確認できているので、設定だけ追加していきます。

以下の通り extends と rules プロパティに追記します。

module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:import/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'next/core-web-vitals',
    'prettier',
  ],
  plugins: ['@typescript-eslint'],
  parserOptions: {
    project: true,
    tsconfigRootDir: __dirname,
  },
  root: true,
  rules: {
    'import/order': [
      'error',
      {
        groups: [
          'builtin',
          'external',
          'internal',
          'parent',
          'sibling',
          'index',
          'object',
          'type',
        ],
        'newlines-between': 'always',
      },
    ],
  },
};

以下の 公式ドキュメントを参照して設定しました。

plugin:import/recommended については、以下の記載がありました。

'recommended' is the combination of these two rule sets ( 'recommended' は次の 2 つのルール セットの組み合わせです。 ) - plugin:import/errors - plugin:import/warnings

import/order はimportの記述順に関するルールを設定できます。
このrulesについてはプロジェクトのディレクトリ構造が具体的に決まってきたらより詳細に設定するのも良いと思います。

package.json の scripts に lint:fix と format を定義する

package.json の scripts に以下の行を追記します。

    "lint:fix": "next lint --fix && format",
    "format": "prettier --write './src/**/*.{ts,tsx}'"

eslint と prettier でコードを自動修正する

以下のコマンドを実行して、コードを自動修正します。

npx lint:fix

VSCode の extentions をインストールする

VSCodeのextentionは似たようなのがあったりするので、私が使用しているものの identifier も書いておきます。

ESlint

Prettier

プロジェクトの VSCode の設定をする

.vscode ディレクトリを作成する

まずディレクトリを作成します。

mkdir .vscode

.vscode/extensions.json を作成する

ファイルを作成します。

vim .vscode/extensions.json

参考までに私は現在以下のような感じで設定しています。適宜必要なものを設定してください。

{
  "recommendations": [
    "dbaeumer.vscode-eslint", // eslint
    "emmanuelbeziat.vscode-great-icons", // iconを変更
    "esbenp.prettier-vscode", // prettier
  ]
}

.vscode/settings.json を作成する

ファイルを作成します。

vim .vscode/settings.json

参考までに私は現在以下のような感じで設定しています。適宜必要なものを設定してください。

{
  "editor.fontFamily": "Cica",
  "editor.fontSize": 18,
  "editor.insertSpaces": true, // Tabキーで半角スペースを入力
  "editor.matchBrackets": "always", // 対応する括弧の強調表示
  "editor.renderWhitespace": "all", // 空白文字を可視化 "all" | "boundary"
  "editor.tabSize": 2, // Tabキーで半角スペースn個分
  "editor.wordWrap": "on", // 1行に収まらない場合は改行して表示する
  "editor.formatOnSave": false, // ファイル保存時、prettier による自動フォーマット
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.fixAll.stylelint": true
  },
  "[graphql]": {
    "editor.formatOnSave": true
  },
  "[javascript]": {
    "editor.formatOnSave": true
  },
  "[javascriptreact]": {
    "editor.formatOnSave": true
  },
  "[json]": {
    "editor.formatOnSave": true
  },
  "[typescript]": {
    "editor.formatOnSave": true
  },
  "[typescriptreact]": {
    "editor.formatOnSave": true
  }
}

今回はここまでにしておきます。

Next.js14の開発環境構築手順 step01 create-next-app --typescript

はじめに

2024年4月時点での自分のためのメモとして、Next.jsの環境構築手順をまとめてみます。

かなり長くなるのでいくつかのstepに分けて書いていきます。

環境

主に以下のような構成でNext.js 14の環境構築を行なっていきます。 CSSを書くにあたってNext.js(React)では多くの選択肢があると思いますが、何か Zero Runtime のCSSライブラリを使用して、環境構築をしていこうと思います。

  • OS: macOS Ventura 13.6.4
  • node 20.5.1
  • npm 9.8.0
  • next 14.2.0
    • eslint + prettier
    • 何か Zero Runtime のCSSライブラリ
    • stylelint
    • lint-staged

nodeのインストール

nodenvか何かでインストールしてバージョンを指定します。

以下のページあたりを参照して、LTS(Long-Term Support 長期サポート)になっているバージョンを選ぶのが良いと思います。

  • GitHub - nodejs/Release: Node.js Release Working Group
  • リリース一覧 | Node.js

2024年4月現在で最新LTSのnode 20系を選択しました。

create next-app

npm を使って環境構築をしていこうと思います。TypeScriptのオプションも使用します。ディレクトリ名は今回は frontend_v2 でやっていますが、任意のディレクトリ名でやってください。

npx create-next-app@latest --ts frontend_v2

対話形式で選択できる初期設定は以下の通り選択しました。

  • ESLintは使う
  • Tailwind CSSは使わない
  • src/ ディレクトリは使う
  • App Routerは使う
  • import aliasはカスタマイズしない(初期値のまま @/*)

package.jsonの確認

nameはご自身の環境に読み替えてください。

{
  "name": "frontend_v2",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "react": "^18",
    "react-dom": "^18",
    "next": "14.2.0"
  },
  "devDependencies": {
    "typescript": "^5",
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "14.2.0"
  }
}

dependenciesとdevDependenciesをアルファベット順に並べ替える

dependenciesとdevDependenciesを以下の通りアルファベット順に並べ替えました。

{
  "name": "frontend_v2",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "14.2.0",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "14.2.0",
    "typescript": "^5"
  }
}

ディレクトリ移動

cd frontend_v2

typesyncをインストールする

typesyncをインストールします。typesyncを実行すると足りない型パッケージをpackage.jsonに追記してくれて便利です。

npm install --save-dev typesync

typesyncを実行する

以下のコマンドを実行します。

npx typesync

以下のように表示されて、@types/eslint がpackage.jsonに追記されたのがわかります。

»  TypeSync v0.12.1
✔  1 new typings added.

📦 frontend_v2 — package.json (1 new typings added)
└─ + @types/eslint

✨  Go ahead and run npm install or yarn to install the packages that were added.

@types/eslint をインストールする

package.json@types/eslint が追記されたのでインストールします。

npm install

TSの絶対パスインポートの設定

tsconfig.json を編集します。

create next-app したときに、pathsが自動で設定されるようになりましたが、 baseUrl は自動では設定されないので自分で設定します。

  "compilerOptions": {
    〜省略〜
    "baseUrl": "./",
    〜省略〜
  },

baseUrlを設定することで、VSCodeの入力補完が利くようになったりするはず。

.eslintrc.json について

json よりも js の方が編集がしやすいですが、 create-next-app によって設定された eslint のバージョンが8系であることと、生成されたファイルが .eslintrc.json なので拡張子はこのままにしておきます。

eslint の設定ファイルについては、今後 eslintrc はではなくなり、Flat Config になるようです。

ESLint を eslintrc から Flat Config に移行する、ハマりポイントを添えて。 #JavaScript - Qiita

この環境構築手順では、 .eslintrc.json のまま進めていきますが、このことは留意しておくと良いと思います。

.eslintrcの拡張子と記述内容を変更する

.eslintrc.json の拡張子を .eslintrc.js に変更します。

.eslintrc.json の記述内容は以下の通りでしたが、

{
  "extends": "next/core-web-vitals"
}

.eslintrc.js に拡張子を変更したので、記述内容を以下の通り変更しました。ついでに extends プロパティの値を配列に変更しています。

module.exports = {
  extends: ["next/core-web-vitals"],
};

長くなりそうなので、今回はここまでにしておきます。

Playwrightを使ってVRTを始める

参照したページ

公式ページ

その他

VRTの概要を予習する

実際に環境構築をする前に「Playwright で一番小さく始める VRT と、次のステップの選択肢 - Speaker Deck」を読んでざっくり概要を予習しました。

「VRT を始める上で考えなければならないこと」のページに書かれていることなど、実践的でとても参考になりました。

環境

  • macOS Monterey 12.6.2
  • node: v18.12.1
  • @playwright/test: 1.42.1
  • @types/node: 20.11.28

ディレクトリを作成して移動する

まずはディレクトリを作成して移動します。

midir playwright
cd playwright

Playwrightをインストールする

以下のコマンドでインストールします。

yarn create playwright

以下の項目を聞かれるので、それぞれ選択しました。

  • TypeScript か JavaScript のどちらを使用するか
    • TypeScript
  • テストフォルダの保存場所
    • tests
  • GitHub Actions の workflow の追加をするか
    • false
  • テストに利用するブラウザを一緒にインストールするか
    • true

以下の通り表示されました。

yarn create v1.22.21
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Installed "create-playwright@1.17.132" with binaries:
      - create-playwright
[####] 4/4Getting started with writing end-to-end tests with Playwright:
Initializing project in '.'
✔ Do you want to use TypeScript or JavaScript? · TypeScript
✔ Where to put your end-to-end tests? · tests
✔ Add a GitHub Actions workflow? (y/N) · false
✔ Install Playwright browsers (can be done manually via 'yarn playwright install')? (Y/n) · true
Initializing Yarn project (yarn init -y)…
yarn init v1.22.21
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.
success Saved package.json
✨  Done in 0.01s.
Installing Playwright Test (yarn add --dev @playwright/test)…
yarn add v1.22.21
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 4 new dependencies.
info Direct dependencies
└─ @playwright/test@1.42.1
info All dependencies
├─ @playwright/test@1.42.1
├─ fsevents@2.3.2
├─ playwright-core@1.42.1
└─ playwright@1.42.1
✨  Done in 3.27s.
Installing Types (yarn add --dev @types/node)…
yarn add v1.22.21
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 2 new dependencies.
info Direct dependencies
└─ @types/node@20.11.28
info All dependencies
├─ @types/node@20.11.28
└─ undici-types@5.26.5
✨  Done in 1.06s.
Writing playwright.config.ts.
Writing tests/example.spec.ts.
Writing tests-examples/demo-todo-app.spec.ts.
Writing package.json.
Downloading browsers (yarn playwright install)…
yarn run v1.22.21
$ /Users/motomichi/Desktop/path/to/your/app/playwrite/node_modules/.bin/playwright install
Downloading Chromium 123.0.6312.4 (playwright build v1105) from https://playwright.azureedge.net/builds/chromium/1105/chromium-mac-arm64.zip
133.1 MiB [====================] 100% 0.0s
Chromium 123.0.6312.4 (playwright build v1105) downloaded to /Users/motomichi/Library/Caches/ms-playwright/chromium-1105
Downloading FFMPEG playwright build v1009 from https://playwright.azureedge.net/builds/ffmpeg/1009/ffmpeg-mac-arm64.zip
1 MiB [====================] 100% 0.0s
FFMPEG playwright build v1009 downloaded to /Users/motomichi/Library/Caches/ms-playwright/ffmpeg-1009
Downloading Firefox 123.0 (playwright build v1440) from https://playwright.azureedge.net/builds/firefox/1440/firefox-mac-13-arm64.zip
76.7 MiB [====================] 100% 0.0s
Firefox 123.0 (playwright build v1440) downloaded to /Users/motomichi/Library/Caches/ms-playwright/firefox-1440
Downloading Webkit 17.4 (playwright build v1983) from https://playwright.azureedge.net/builds/webkit/1983/webkit-mac-12-arm64.zip
64 MiB [====================] 100% 0.0s
Webkit 17.4 (playwright build v1983) downloaded to /Users/motomichi/Library/Caches/ms-playwright/webkit-1983
✨  Done in 108.25s.
✔ Success! Created a Playwright Test project at /Users/motomichi/Desktop/path/to/your/app/playwrite

Inside that directory, you can run several commands:

  yarn playwright test
    Runs the end-to-end tests.

  yarn playwright test --ui
    Starts the interactive UI mode.

  yarn playwright test --project=chromium
    Runs the tests only on Desktop Chrome.

  yarn playwright test example
    Runs the tests in a specific file.

  yarn playwright test --debug
    Runs the tests in debug mode.

  yarn playwright codegen
    Auto generate tests with Codegen.

We suggest that you begin by typing:

    yarn playwright test

And check out the following files:
  - ./tests/example.spec.ts - Example end-to-end test
  - ./tests-examples/demo-todo-app.spec.ts - Demo Todo App end-to-end tests
  - ./playwright.config.ts - Playwright Test configuration

Visit https://playwright.dev/docs/intro for more information. ✨

Happy hacking! 🎭
✨  Done in 170.12s.

生成されたディレクトリとファイルを確認する

以下のようなディレクトリとファイルが生成されました。

.
├── node_modules
│   ├── @playwright
│   ├── @types
│   ├── fsevents
│   ├── playwright
│   ├── playwright-core
│   └── undici-types
├── package.json
├── playwright.config.ts
├── tests
│   └── example.spec.ts
├── tests-examples
│   └── demo-todo-app.spec.ts
└── yarn.lock

テストを実行してみる

サンプルのテストを実行してみます。

yarn の場合は以下のコマンドで実行できます。

yarn playwright test

npm の場合は以下のコマンドで実行できます。

npx playwright test

以下の通り6件のテストが成功しました。

yarn run v1.22.21
$ /Users/motomichi/Desktop/path/to/your/app/playwrite/node_modules/.bin/playwright test

Running 6 tests using 4 workers
  6 passed (12.0s)

To open last HTML report run:

  yarn playwright show-report

✨  Done in 12.78s.

レポートを確認する

以下のコマンドを実行します。

yarn playwright show-report

ブラウザで http://localhost:9323/ が開いて、以下のようなページが表示されます。

リストのアイテムをクリックすると、詳細が表示されます。

生成されたディレクトリとファイルを再度確認する

以下のように playwright-report ディレクトリと test-results ディレクトリが生成されました。

.
├── node_modules
│   ├── @playwright
│   ├── @types
│   ├── fsevents
│   ├── playwright
│   ├── playwright-core
│   └── undici-types
├── package.json
├── playwright-report
│   └── index.html
├── playwright.config.ts
├── test-results
├── tests
│   └── example.spec.ts
├── tests-examples
│   └── demo-todo-app.spec.ts
└── yarn.lock

playwrite/tests/snapshot.spec.ts の作成とその記述内容

ファイルを作成します。

vim tests/snapshot.spec.ts

Visual comparisons | Playwright の例から、ファイル名を snapshot.png で指定するように変更して、以下の通り記述します。

import { test, expect } from '@playwright/test';

test('example test', async ({ page }) => {
  await page.goto('https://playwright.dev');
  await expect(page).toHaveScreenshot('snapshot.png');
});

実行してみる

以下のコマンドを実行します。

yarn playwright test tests/snapshot.spec.ts

実行結果

公式ページの例にあるように、以下のエラーになりました。

Error: A snapshot doesn't exist at /Users/motomichi/Desktop/path/to/your/app/playwrite/tests/snapshot.spec.ts-snapshots/snapshot-webkit-darwin.png, writing actual.

初回のテストは比較対象となる golden file が無いため失敗します。という趣旨のことが書かれています。

golden file の確認

toHaveScreenshot() の引数に 'snapshot.png' を渡したので、テストが実施されたブラウザ毎に以下の3ファイルが生成されました。これらが次回の実行時に golden file として使用されます。

golden file を更新する

ページを編集したときに、意図した変更であることが確認できたら、 golden file を更新します。

以下のように --update-snapshots フラグを付けて実行すると更新できます。

yarn playwright test tests/snapshot.spec.ts --update-snapshots

その他

公式ページに記載されていたことについて軽く補足しておきます。

  • toHaveScreenshot() メソッドには配列を渡すことができる。
  • toHaveScreenshot() メソッドには、 pixelmatch の様々なオプションを渡すことができる。
  • toHaveScreenshot() メソッドは画像だけでなく、テキストまたは任意のバイナリ データの比較にも使用できる。

公式ページを参照することをおすすめします。 Visual comparisons | Playwright

Uncaught ReferenceError: __VUE_PROD_DEVTOOLS__ is not defined を解決する

参照したページ

環境

私の環境で今回の件に関係ありそうなパッケージのバージョンは以下の通りです。

  • webpack: 4.46.0
  • vue: 3.4.21
  • vue-router: 4.3.0

経緯

Vue2からVue3にアップグレードする作業をして、Railsのsystem specをローカルで実行すると全て成功するのに、git pushしてbuildkiteで実行すると全て失敗していました。

capybaraでconsoleの出力をしてみると以下のエラーが出力されました。

Uncaught ReferenceError: __VUE_PROD_DEVTOOLS__ is not defined

修正する

私の場合は webpack.config.js の resolve.alias で vue.esm-browser.js を設定していたのが間違いで、 vue.esm-bundler.js に修正しました。

以下の通り設定しました。

  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm-bundler.js',
    },
  },

その他

それでも直らないとか困った場合は vue.js - ReferenceError: __VUE_PROD_DEVTOOLS__ is not defined - Stack Overflow にあるように、以下の通り定数を定義すると一応修正できそうな気がします。

     new webpack.DefinePlugin({
       '__VUE_PROD_DEVTOOLS__': JSON.stringify(false),
     }),

Railsのsystem spec (Capybara)でJavaScriptのconsole.logを出力する方法

参照したページ

gem のバージョン

gem 'selenium-webdriver', '~> 4.11.0'
 
gem 'capybara', '>= 2.15'

出力する方法

以下のように書くと出力されました。

puts page.driver.browser.logs.get(:browser)

おまけ

その1

参照した Capybara+ChromeでJavaScriptのエラーを出力 #Rails - Qiita の通りに

以下のように書くと

puts page.driver.browser.manage.logs.get(:browser)

以下のようなエラーが出ました。

NoMethodError:
       undefined method `logs' for #<Selenium::WebDriver::Manager:0x0000000109cb6ce0 @bridge=#<Selenium::WebDriver::Remote::Bridge:0x0000000109c7f740 @http=#<Capybara::Selenium::PersistentClient:0x0000000109c67460 @open_timeout=nil, @read_timeout=nil, @server_url=#<URI::HTTP http://127.0.0.1:9515/>

その2

以下のように書くと

puts page.driver.browser.manage

以下のように出力されました。

#<Selenium::WebDriver::Manager:0x0000000109df9c88>

その3

以下のように書くと

puts page.driver.browser.logs

以下のように出力されました。

#<Selenium::WebDriver::Logs:0x000000010e74fec8>

Vue2からVue3への移行作業で出た Failed to resolve component: router-view If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement. を解決する

解決方法

以下のようになっている router プロパティを

import { createApp } from 'vue';
import router from './router.js';
import App from './app.vue';

const app = createApp({
  router,
  components: {
    App,
  },
});

app.mount('#app_regular_users_credit_card');

以下のように app.use() を使うように変更しました。

import { createApp } from 'vue';
import router from './router.js';
import App from './app.vue';

const app = createApp({
  components: {
    App,
  },
});

app.use(router);
app.mount('#app_regular_users_credit_card');

Vue2からVue3への移行作業で出た Error: Provide the "history" option when calling "createRouter()" を解決する

参照したページ

概要

vue-router を使っているアプリケーションを移行する際に発生するエラーです。

ブラウザのコンソールに出力されたエラー

Error: Provide the "history" option when calling "createRouter()"

解決方法

以下のように createRouter している箇所を

import { createRouter } from 'vue-router';
const router = createRouter({
    routes,
});

以下のように変更しました。

import { createRouter , createWebHashHistory } from 'vue-router';
const router = createRouter({
    history:createWebHashHistory(),
    routes
})

参照した2ページはそれぞれ、以下のメソッドを使用しています。

  • createWebHashHistory
  • createWebHistory

違いを調べてご自身のアプリケーションに適した方を使用してください。