react安装依赖引入报错

在react脚手架中,使用npm add antd安装最新的依赖以后,再使用import 'antd/dist/antd.css'全局引入会报错,报错原因显示为:

Module not found: Error: Can't resolve 'antd/dist/antd.css' in 'C:\Users\26396\Desktop\react-project\src'

这时进入下载的antd依赖文件中查看会发现找不到antd.css的文件,原因是使用默认add antd下载的是最新版本的依赖文件,如果要使用上方口令引入css样式文件,需要安装前一个版本的依赖,首先把原本的依赖卸载,使用的口令是:

npm remove antd

然后安装上一个版本的依赖,口令为:

npm add antd@^4.24.2

之后就可以正常引入css样式的文件,并且可以正常使用。

在引入时,引入如下:

import { Button } from 'antd';
import 'antd/dist/antd.css' 
在终端npm start后控制台报错:

Failed to parse source map: 'webpack://antd/./components/config-provider/style/index.less' URL is not supported
 
Failed to parse source map: 'webpack://antd/./components/icon/style/index.less' URL is not supported
 
Failed to parse source map: 'webpack://antd/./components/locale-provider/style/index.less' URL is not supported
 
Failed to parse source map: 'webpack://antd/./components/time-picker/style/index.less' URL is not supported
 
Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.
 
WARNING in ./node_modules/antd/dist/antd.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./node_modules/antd/dist/antd.css)
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map: 'webpack://antd/./components/config-provider/style/index.less' URL is not supported
 
WARNING in ./node_modules/antd/dist/antd.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./node_modules/antd/dist/antd.css)
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map: 'webpack://antd/./components/icon/style/index.less' URL is not supported
 
WARNING in ./node_modules/antd/dist/antd.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./node_modules/antd/dist/antd.css)
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map: 'webpack://antd/./components/locale-provider/style/index.less' URL is not supported
 
WARNING in ./node_modules/antd/dist/antd.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./node_modules/antd/dist/antd.css)
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map: 'webpack://antd/./components/time-picker/style/index.less' URL is not supported


解决方法:
因为引入的import 'antd/dist/antd.css'不对,应该引入import 'antd/dist/antd.min.css',注意全局查找引入antd样式的地方,进行全局修改,修改之后,报错消失,npm start正常运行。
 

猜你喜欢

转载自blog.csdn.net/qq_45059691/article/details/128051615