Create-React-App:从 node_module 目录包含 CSS 的最佳方式是什么

标签 create-react-app

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

我试过:import '/node_modules/packagename/css/styles.css';
但我得到了错误:

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 更新。我可以进行符号链接(symbolic link),但是当我在 Windows 上开发并部署到 linux 时,这并不理想。

我包含 CSS 的最佳方式是什么?

最佳答案

找到css文件的路径

示例:./node_modules/packagename/dist/css/styles.css
使用与 node_modules 相关的路径导入(node_modules/ 之后的任何内容)

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

关于Create-React-App:从 node_module 目录包含 CSS 的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45658500/

相关文章:

reactjs - 创建 React 应用程序抛出 "Loading chunk # failed"错误

azure-devops - 如何在 create-react-app 项目的根目录中获取 Web.config 文件以复制到构建文件夹,以便对其进行存档和部署

reactjs - 将 create-react-app 部署到 Plesk Onyx

javascript - 在 React Bootstrap 中,我想用 html 格式化我的文本,但不能

javascript - 更改 node_modules 中的包时如何触发 React 的热重载?

javascript - 如何仅在 create-react-app 的生产构建文件中包含 JavaScript 库?

reactjs - CRA : Extract particularly big modules into separate chunks?

reactjs - Ant Design with sass in React with create-react-app

Reactjs:如何在生产应用程序的开发工具中隐藏节点模块和webconfig?

javascript - 如何将图像从 JSON 数据导入到 create-react-app