简体   繁体   English

Create-react-app '编译失败。' 由于 css 导入

[英]Create-react-app 'Failed to compile.' due to css import

Appearing out of nowhere, this error has me stomped.这个错误无处不在,让我感到震惊。 It appears any time I try to import a css file, whether in full or as a module.每当我尝试导入 css 文件时,无论是完整文件还是作为模块,它都会出现。

Eg例如

import "./index.css"

or或者

import styles from "./button.module.css"

It MAY have been due to the latest macOS beta at least it seems to have coincided with its release.这可能是由于最新的 macOS beta 至少它似乎与它的发布同时发生。 I tried rolling back from it, but that didn't work.我尝试从中回滚,但这没有用。

Failed to compile
./src/index.css (./node_modules/css-loader/dist/cjs.js?modules=true!./node_modules/style-loader/dist/cjs.js!./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css)
CssSyntax error: Unknown word (1:1)

> 1 | var api = require("!../node_modules/style-loader/dist/runtime/injectStylesIntoStyleTag.js");
    | ^
  2 |             var content = require("!!../node_modules/css-loader/dist/cjs.js??ref--6-oneOf-4-1!../node_modules/postcss-loader/src/index.js??postcss!./index.css");
  3 | 
This error occurred during the build time and cannot be dismissed.

I've tried manually adding web pack, checked everything for syntax errors, re-installed the whole repo, went back several commits and so on.我试过手动添加 web 包,检查所有语法错误,重新安装整个 repo,返回几次提交等等。 Nothing seems to do anything.似乎什么都做不了。 Happy to supply more context.很高兴提供更多背景信息。

(I found one other person mentioning the same problem on SO 15 hours ago, but he hadn't supplied debugging information so the ticket was closed). (我在 15 小时前发现另一个人在 SO 上提到了同样的问题,但他没有提供调试信息所以票被关闭了)。

Note: The same codebase runs perfectly well on my MacBook, no problems!注意:相同的代码库在我的 MacBook 上运行得很好,没有问题!

It's seems like your npm project setup is broken.您的 npm 项目设置似乎已损坏。 This type of problem famously called Failed to compile generally occurs when we are moving one branch to another branch more often and we sometimes forgot to do npm install and continue to work on another branch which has new packages without doing npm install and many more cases which is very hard to tell that in which cases your npm setup is broken.当我们更频繁地将一个分支移动到另一个分支并且我们有时忘记执行npm install并继续在另一个具有新包的分支上工作而不执行 npm 安装时,这种类型的问题通常称为Failed to compile以及更多情况很难说在哪些情况下您的 npm 设置已损坏。

There are couple of ways of solving this issue.有几种方法可以解决这个问题。

First way第一种方式

  • Delete the node_modules directory and package-lock.json file.删除node_modules目录和package-lock.json文件。
  • Close the editor and open it again and in new terminal do npm i .关闭编辑器并再次打开它,在新终端中执行npm i Then check whether it is compiled or not.然后检查它是否被编译。

If that didn't work then follow the second way.如果那不起作用,请按照第二种方式进行。

Second way第二种方式

Clear the npm cache forcefully of your project setup.强制清除项目设置的 npm 缓存。

npm cache clear -f

Then restart the system for closing all the node service for this session. After restart do npm i and check again.然后重启系统关闭这个session的所有节点服务,重启后执行npm i再检查。

If that didn't work then follow the third way which I think that fail compilation issue will be easily solved.如果那不起作用,那么按照我认为编译失败问题很容易解决的第三种方法。

Third way第三种方式

Just clone your project again in another directory and do npm install .只需在另一个目录中再次克隆您的项目并执行npm install

The code is right.代码是对的。 There may not be a problem with code.代码可能没有问题。

The steps mentioned below might help:下面提到的步骤可能会有所帮助:

  1. Delete node_modules folder.删除node_modules文件夹。
  2. run npm install运行npm install
  3. run npm cache clean -f (try without -f first)运行npm cache clean -f (先尝试不使用-f

*first 2 steps worked for me. *前两个步骤对我有用。

Can you try this?你能试试这个吗?

Delete these folders: node_modules and package-lock.json .删除这些文件夹: node_modulespackage-lock.json (You can also delete a cache folder if there is) (如果有缓存文件夹也可以删除)

Run this Commands:运行此命令:

With yarn: yarn配纱: yarn

With npm: npm install用 npm: npm install

You can also try to change your import method:您也可以尝试更改您的导入方法:

import * as styles from...

This might help hopefully!这可能会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM