62

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?

4 Answers 4

74

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'
Sign up to request clarification or add additional context in comments.

3 Comments

you should definitely not be using relative paths to anything in the node_modules folder. node_modules css in create-react-app can be imported the same way as any js package in the node modules folder
Correct! If the css loader is defined properly, it should load the css from the package without the relative path.
@AfzalHossain Perhaps you should update your answer accordingly?
52

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'

when using (s)css imports, use the tilde (~) to indicate an absolute import:

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

2 Comments

Yes, mostly the css will be in <package>/dist folder
how can I load a css file using <Link> from node_modules without copying it into "public" folder
11

A distinction not made from the previous answers is it depends on where you're importing the CSS into; a component or into a stylesheet.

If you're importing a node_modules stylesheet into a component, you don't need a relative path like mentioned above.

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

However, if you're importing a node_modules stylesheet into a CSS/SCSS file, you need to use tilde ~.

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

Comments

-7

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.