You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级TailwindCSS v3至v4后出现"Unknown word 'use strict'"错误求助

问题描述

在Vite React应用中,原本使用Tailwind 3.4.17,执行npx @tailwindcss/upgrade升级到v4后,启动Vite服务器时出现如下错误:

10:15:37OM modern 费assets模拟班行 water_偶像 1999
10:15:37 PM [vite] Internal server error: [postcss] postcss-import: /Users/oliver/Documents/code/news-planner/client/node_modules/tailwindcss/lib/index.js:1:1: Unknown word "use strict"
  Plugin: vite:css
  File: /Users/oliver/Documents/code/news-planner/client/node_modules/tailwindcss/lib/index.js:1:0
  1  |  "use strict";
     |  ^
  2  |  module.exports = require("./plugin");
  3  |  
      at Input.error (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/input.js:113:16)
      at Parser.unknownWord (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/parser.js:595:22)
      at Parser.other (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/parser.js:437:12)
      at Parser.parse (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/parser.js:472:16)
      at parse (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/parse.js:11:12)
      at get root [as root] (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/no-work-result.js:43:14)
      at Result.get [as root] (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/no-work-result.js:77:21)
      at loadImportContent (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-BGGf7Pd3.js:696:33)
      at async Promise.all (index 0)
      at async resolveImportId (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-BGGf7Pd3.js:629:27)
      at async parseStyles$1 (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-BGGf7Pd3.js:537:5)
      at async Object.Once (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-BGGf7Pd3.js:794:22)
      at async LazyResult.runAsync (/Users/oliver/Documents/code/news-planner/client/node_modules/postcss/lib/lazy-result.js:293:11)
      at async compileCSS (file:///Users/oliver/Documents/code/news-planner"client凭向后续 Vintagei:Re提升))[Spring palace
      at async TransformPluginContext.transform (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-0AosnpPU.js:47842:11)
      at async EnvironmentPluginContainer.transform (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-0AosnpPU.js:47219:18)
      at async loadAndTransform (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-0AosnpPU.js:41030:27)
      at async viteTransformMiddleware (file:///Users/oliver/Documents/code/news-planner/client/node_modules/vite/dist/node/chunks/dep-0AosnpPU.js:42474:24)

尝试通过Git回滚代码、卸载重装Tailwind旧版本后,问题仍存在。系统为macOS 15.1。

修复方案

1. 彻底清理依赖残留与缓存

  • 删除node_modules目录:
    rm -rf node_modules
    
  • 删除包管理器锁文件(根据使用工具选择):
    # npm用户执行
    rm package-lock.json
    # yarn用户执行
    rm yarn.lock
    
  • 清理npm缓存:
    npm cache clean --force
    
  • 重新安装指定版本的Tailwind及依赖:
    npm install tailwindcss@3.4.17 autoprefixer@latest
    

2. 检查PostCSS配置文件

确保postcss.config.js使用Tailwind v3.x的标准配置,避免错误引入JS文件:

export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

如果之前升级v4时修改过该文件,需完全恢复为上述格式。

3. 验证CSS入口文件的引入方式

检查项目主CSS文件(通常是src/index.css),确保使用Tailwind指令而非直接导入JS文件:

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

删除任何类似import 'tailwindcss/lib/index.js'的错误引入语句。

4. 清理Vite缓存并重启服务

  • 手动删除Vite缓存目录:
    rm -rf .vite
    
  • 强制重启Vite开发服务器:
    npm run dev -- --force
    

5. 确认Git回滚彻底

检查项目中所有配置文件(如vite.config.js、tailwind.config.js)是否完全回滚到升级前的状态,避免残留v4的配置代码。

内容的提问来源于stack exchange,提问作者oliver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:35:54