How to reference CSS from libraries installed via npm?


Tags: javascript,html,css,node.js,npm

Problem :

So for example say I installed something:

npm install --save something

and it downloads into

./node_modules/something/

and it has a folder in there perhaps called styles and in that folder you have something.css. How would I include that css file in my html document (if my html document was along-side the node-modules folder?

I mean I could do this in my html head:

<link rel="stylesheet" href="./node_modules/something/styles/something.css" type="text/css" media="screen" />

But it feels wrong to go dig in your node_modules directory for stuff. Especially if the html document perhaps needs to get minified and then thrown into some ./dist/ directory. Because then the path to something.css is off..

Isn't there a way to simply just go:

<link rel="stylesheet" href="something.css" type="text/css" media="screen" />

in your html document - regardless of where it's sitting in your project structure - and it'll just know where to go find that css file?



Solution :

There is a package for this called npm-css

In webpack you can require css like require('bootstrap.css'), this is why installing css via npm quite useful

if you don't have it, you can create a npm script task that would require (with fs.readFile) all the css files from the node_modules, compile them into single file (which is what npm-css does)


    CSS Howto..

    How can I centralize this jquery styling colors into css?

    How to force spaces between 2 elements when position:absolute with CSS only?

    Having css issues div ahref showing up behind others, domino effect

    CSS: how do I force contents in a table cell to stay on one line when browser size reduced

    How to shorten blockquote to wrap around floated div?

    CSS how to align elements inside a div with 2 columns

    how to set css to a specific attribute of an input tag

    How do I get two side by side divs displayed correctly inside a bordered, dynamic height content area?

    How to get a website's position setup in a way that an image is centred?

    How do I overwrite certain conditions when you have two CSS files?

    How can

    be centered and justified?

    How to save game information PHP/HTML/CSS/JS [closed]

    How to have background between two handles of jQuery Slider using css gradient

    attaching div to a specific element for showing with javascript

    How to prioritize custom css over the default Angular-material css

    How to exclude particular class name in CSS selector?

    How do I display all the data in a MySQL table in a theme? [closed]

    How to rotate an element around a line other than X=0,Y=0 and Z=0 using CSS3?

    How to access an object from a webpage in selenium

    How set css style to GWT TextBox text?

    How to do img validation

    Bootstrap table. How to remove all borders from table?

    css margin-left propery - how to specify for various browsers [closed]

    How to get the style value using attr

    How to link index.html to css file for github page

    How can I align two different-size pieces of text to the left and right while matching baselines?

    How to adjust navbar to a fixed width in Twitter Bootstrap?

    How to center text horizontally and vertically within box element? [duplicate]

    How to render text in .NET in the same size as browsers does given CSS for the text

    Slideshow using Jquery, images do not fit window