Vite PostCSS module error when building app in Svelte
Asked Answered
T

6

7

I came across this strange error in Svelte; every time I ran npm run dev, this vite error would appear:

[vite] Internal server error: Failed to load PostCSS config (searchPath: /Users/Documents/Personal projects): [Failed to load PostCSS config] Failed to load PostCSS config (searchPath: /Users/Documents/Personal projects): [Failed to load PostCSS config] Failed to load PostCSS config (searchPath: /Users/Documents/Personal projects): [Error] Cannot find module 'autoprefixer'

I'm new to vite so it took me an hour of research, to figure out how to export the module, I was able to fix it by creating a postcss.config.cjs file and inside the file add:

module.exports = {
    autoprefixer: {}
}

I hope this helps anyone that comes across the same/similar error.

Theory answered 27/7, 2022 at 10:41 Comment(0)
T
15

Create postcss.config.cjs file and add:

module.exports = {
    autoprefixer: {}
}

The reason why it wasn't building, is because svelte wasn't exporting the autoprefixer module

Theory answered 27/7, 2022 at 10:43 Comment(1)
CommonJS files when using vite need to be explicitly named as .cjs. See issue on GitHubAmoritta
W
2

I solved this issue by deleting the node_modules directory, and ran npm i again.

Whitish answered 31/10, 2022 at 17:38 Comment(0)
B
1

I had similar problem while following a tutorial and i fixed this way.

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

npx tailwindcss init -p

npm i

and then finally

npm run dev
Bethlehem answered 29/11, 2023 at 22:13 Comment(0)
C
1
  1. Go to your Root folder (Root folder contains. folders like src)
  2. Create a new file, name it postcss.config.js
  3. Paste this into the postcss.config.js file

module.exports = {
        autoprefixer: {}
    }
  1. Save, delete npm_modules folder if its there
  2. go to root folder using cd in terminal, run npm install
  3. run ng serve
Congeal answered 5/12, 2023 at 12:7 Comment(0)
M
0

Just to add this to your package.json

"type": "module",

about "scripts"

Martita answered 21/5, 2023 at 13:19 Comment(0)
S
0

You can also switch to entirely TypeScript

postcss.config.ts

import tailwindConfig from './tailwind.config'
import autoprefixer from 'autoprefixer'
import tailwind from 'tailwindcss'

export default {
  plugins: [tailwind(tailwindConfig), autoprefixer],
}

Update vite.config.ts with postcss as a css config

...
import postcss from './postcss.config'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  css: {
    postcss,
  },
})

Update tsconfig.node.json to include the config

{
  ...
  "include": [
    "vite.config.ts",
    "postcss.config.ts",
    "tailwind.config.ts"
  ]
}

Example From GitHub

Scheller answered 7/12, 2023 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.