0
0

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にTailwindcssを導入する

0
Posted at

Next.jsにTaillwindowの導入方法。

基本的に公式サイトを見ればいけるが初歩的なところでミスったのでメモで残しておこうと思う。
https://tailwindcss.com/docs/guides/nextjs

1.下記のコマンドを実行

npm install -D tailwindcss@latest postcss@latest autoprefixer@latest

2.下記のコマンドを実行して、tailwind.config.js と postcss.config.jsファイルを作成する。
tailwind.config.jsにオリジナルのカスタマイズを記載する。

npx tailwindcss init -p

3.tailwind.config.jsのpurgeを下記に変更する。

  module.exports = {
   purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
    darkMode: false, // or 'media' or 'class'
    theme: {
      extend: {},
    },
    variants: {
      extend: {},
    },
    plugins: [],
  }

4../styles/globals.cssを下記に変更

@tailwind base;
@tailwind components;
@tailwind utilities;

5.pages/_app.jsにimportを追記

//追加
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

導入した際に起こったエラー

./styles/globals.css
Global CSS cannot be imported from files other than your Custom <App>. Due to the Global nature of stylesheets, and to avoid conflicts, Please move all first-party global CSS imports to pages/_app.js. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://nextjs.org/docs/messages/css-global
Location: pages/index.js

エラーの内容はカスタム<App>以外のファイルからグローバルCSSをインポートすることはできませんということだった。
要は_app.js以外のファイルでimport '../styles/globals.css'しちゃダメだよと言っている。
index.jsxにもimport '../styles/globals.css'を記載していたので、上記のエラーが出てしまっていた。

ちゃんとドキュメントを読むことを意識しないとな〜。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?