Create-React-App: What's the best way to inclu

2019-04-18 05:51发布

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.

I tried: import '/node_modules/packagename/css/styles.css';

But I get the error:

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/.

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.

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

3条回答
不美不萌又怎样
2楼-- · 2019-04-18 06:23

I found this a bug of a pain. specially my webpack.config.js is not running anymore.

specially react app is now running in src folder and the import files need to be in the public folder.

i was using materialize-social and found out the easiest way is to move node_module file folder "materialize-social" to the public directory any other way please commend it down.

查看更多
贪生不怕死
3楼-- · 2019-04-18 06:30

relative paths are unnecessary from node_modules and should not be the recommended way to include the css

all you have to do is leave off the preceding slash and node_modules directory same as importing a js package from node modules:

import 'package/css/style-to-import.css'

查看更多
地球回转人心会变
4楼-- · 2019-04-18 06:34

Find the path of the css file

example: ./node_modules/packagename/dist/css/styles.css

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

import 'packagename/dist/css/styles.css'
查看更多
登录 后发表回答