Create-React-App:从node_module目录包含CSS的最佳方法是什么 [英] Create-React-App: What's the best way to include CSS from node_module directory

查看:419
本文介绍了Create-React-App:从node_module目录包含CSS的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的create-react-app项目中包含一些CSS。 CSS来自第三方NPM软件包,因此它位于node_modules目录中。

I'm trying to include some CSS in my create-react-app project. The CSS is from a 3rd party NPM package and therefore its in the node_modules directory.

我尝试过:
import'/ node_modules / packagename / css / styles.css';

但是我得到了错误:

未找到模块:您试图导入/node_modules/packagename/css/styles.css,它不在项目src /目录下。不支持src /以外的相对导入。您可以将其移动到src /中,也可以从项目的node_modules /中向其添加符号链接。

Module not found: You attempted to import /node_modules/packagename/css/styles.css which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.

我宁愿不移动CSS到src /,因此可以通过NPM更新。我可以进行符号链接,但是当我在Windows上开发并部署到linux时,这并不理想。

I would prefer to not move the CSS to src/ so it can be updated via NPM. I could symlink but as I develop on windows and deploy to linux this isn't ideal.

从我的角度来看,包括CSS的最佳方法是什么?

What's the best way from me to include the CSS?

推荐答案

查找css文件的路径

示例: ./node_modules/packagename/dist/css/styles.css

使用与node_modules相关的路径( node_modules / )

Import using the path related to node_modules (anything after node_modules/ )

import 'packagename/dist/css/styles.css'

这篇关于Create-React-App:从node_module目录包含CSS的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆