How to reference a scss files installed to node_modules?
How to import css file from node_modules?
Import a SCSS files from Node Modules
node.js - Importing Sass through npm - Stack Overflow
After having the same issue, I got confused with all the answers here and the comments over the repository of sass in github.
I just want to point out that as December 2014, this issue has been resolved. It is now possible to import css files directly into your sass file. The following PR in github solves the issue.
The syntax is the same as now - @import "your/path/to/the/file", without an extension after the file name. This will import your file directly. If you append *.css at the end, it will translate into the css rule @import url(...).
In case you are using some of the "fancy" new module bundlers such as webpack, you will probably need to use use ~ in the beginning of the path. So, if you want to import the following path node_modules/bootstrap/src/core.scss you would write something like @import "~bootstrap/src/core".
NOTE:
It appears this isn't working for everybody. If your interpreter is based on libsass it should be working fine (checkout this). I've tested using @import on node-sass and it's working fine. Unfortunately this works and doesn't work on some ruby instances.
This was implemented and merged starting from version 3.2 (pull #754 merged on 2 Jan 2015 for libsass, issues originaly were defined here: sass#193 #556, libsass#318).
To cut the long story short, the syntax in next:
to import (include) the raw CSS-file
the syntax is **without `.css`** extension at the end (results in actual read of partial `s[ac]ss|css` and include of it inline to SCSS/SASS):@import "path/to/file";to import the CSS-file in a traditional way
syntax goes in traditional way, **with `.css` extension** at the end (results to `@import url("path/to/file.css");` in your compiled CSS):@import "path/to/file.css";
And it is damn good: this syntax is elegant and laconic, plus backward compatible! It works excellently with libsass and node-sass.
__
To avoid further speculations in comments, writing this explicitly: Ruby based Sass still has this feature unimplemented after 7 years of discussions. By the time of writing this answer, it's promised that in 4.0 there will be a simple way to accomplish this, probably with the help of @use. It seems there will be an implementation very soon, the new "planned" "Proposal Accepted" tag was assigned for the issue #556 and the new @use feature.
UPD: on 26 October 2020 lib-sass was deprecated, therefore issue #556 was immediately closed.
__
answer might be updated, as soon as something changes.
I'm trying to load some styles from the node_modules folder, but it's really difficult. And it works fine when i import bootstrap:
@import 'bootstrap/scss/bootstrap-reboot';
but when i import date-picker bootstrap it throw error:
@import "ngx-bootstrap/datepicker/bs-datepicker"
Angular-16 ,
"bootstrap": "^5.3.3", "ngx-bootstrap": "^11.0.2",
If you are looking for a handy answer in 2017 and are using Webpack, this was the easiest I found.
Suppose your module path is like:
node_modules/some-module/sass/app
Then in your main scss file you can use:
@import "~some-module/sass/app";
Tilde operator shall resolve any import as a module.
You can add another includePaths to your render options.
Plain example
Snippet based on example from Oncle Tom.
var options = {
file: './sample.scss',
includePaths: [
path.join(__dirname, 'bower_components'), // bower
path.join(__dirname, 'node_modules') // npm
]
};
sass.render(options, function(err, result){
console.log(result.css.toString());
});
That should do. You can include the files from package using @import "my-cool-package/super-grid
Webpack and scss-loader example
{
test: /\.scss$/,
loader: 'style!css!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap=true&sourceMapContents=true&includePaths[]=./node_modules'
},
Notice the last argument, includePaths has to be array. Keep in mind to use right format
You can import files relative to your project's root (resolving node_modules/ from the root folder) by prefixing with a tilde ~:
@import '~react-select/dist/react-datetime.css';
This is a poorly documented Webpack (a redundant phrase) convention, see https://github.com/webpack-contrib/css-loader/issues/12#issuecomment-41940311 and What does a `~` tilde in a CSS `url()` do?
I had a similar issue today. After all, all I had to do was to configure resolve in my webpack config file. I hope this will help somebody.
Webpack version I used:
"webpack": "^4.37.0",
In a webpack config file, the following should be configured:
module.exports = {
resolve: {
extensions: ['.json', '.js', '.jsx'],
modules: ['node_modules'],
},
or
module.exports = {
resolve: {
alias: {
'some-library': path.resolve(__dirname, './node_modules/some-library'),
}
},
In a css file, we can access a library by a relative path from node_modules:
@import '~some-library/src/some-css-file';
I am trying to import a (s)css file in my library component's scss file.
The import is the really standard:
"@import '~@syncfusion/ej2-angular-grids/styles/material.css"
However when building the library (ng build) and using it, the style is not applied.
Is there anything I am missing?
» npm install node-sass-package-importer
SASS compiler doesn't know where to look for the files. The location needs to be specified.
gulp.task('sass', function () {
return gulp.src(['source/assets/css/**.scss', '!source/assets/css/**/_*.[scss|sass'])
.pipe(sass({
includePaths: ['node_modules']
}))
.pipe(gulp.dest('output/assets/css'));
});
What works for me, in 2020, is this:
function styles() {
return (
gulp.src(paths.styles.src)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: ['./node_modules/purecss-sass/vendor/assets/stylesheets/',
'./node_modules/modularscale-sass/stylesheets/',
'./node_modules/typi/scss/'
]
}))
.on("error", sass.logError)
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(sourcemaps.write())
.pipe(gulp.dest(paths.styles.dest))
.pipe(browserSync.stream())
);
}
Now in the scss files, I can
@import 'modularscale';
@import 'typi';
@import 'purecss';
The other options seem to be:
- put the full paths to the main
_somelibrary.scssfile directly in the scss files (minus the extension), so something like:
@import '../../node_modules/purecss-sass/vendor/assets/stylesheets/_purecss';
- Put
includePaths: ['./node_modules']and add the relative paths in the scss files:
@import 'purecss-sass/vendor/assets/stylesheets/_purecss';
If you are using Webpack 4.x, then all that you need is to add a couple of aliases in your Webpack config file like this:
// Your Webpack config file
...
webpackFinal: async config => {
config.module.resolve.alias = {
'animate-css': path.resolve(__dirname, '../../../node_modules/animate.css'),
'react-table': path.resolve(__dirname, '../../../node_modules/react-table-v6/react-table.css'),
}
...
}
and then you can import these aliases in your main.scss:
@import 'animate-css';
@import 'react-table';
You also need to remove options from your sass-loader. I specified paths to node_modules assuming it is on the same level as your app directory, also package react-table contains no CSS file, so I specified v6.
Complete working Webpack 5 example with includePaths
Here's a complete example, not sure why includePaths didn't work for OP, or if there were other restrictions, but it did the trick for me, as I'm able to directly @use "normalize.css/normalize.css" from main.scss without the tilde.
This produces dist/main.css which is a minified CSS containing both:
- our
main.scss normalize.cssvianode_modules
The effect of both can be seen on the index.html test file.
webpack.config.js
const path = require('path');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
module.exports = {
entry: {
main: ['./main.scss'],
},
mode: 'none',
module: {
rules: [
{
test: /\.(scss|css)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: "sass-loader",
options: {
sassOptions: {
includePaths: [nodeModulesPath],
},
},
},
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
],
optimization: {
minimizer: [
new CssMinimizerPlugin(),
],
minimize: true,
}
};
main.scss
@use "normalize.css/normalize.css";
body {
background-color: red;
}
index.html
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Sass import</title>
<link rel="stylesheet" href="dist/main.css">
</head>
<body>
<p>Hello</p>
</body>
</html>
package.json
{
"name": "webpack-cheat",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "webpack",
"sass": "rm -f dist/main.css && sass main.scss dist/main.css"
},
"author": "Ciro Santilli",
"license": "MIT",
"devDependencies": {
"css-loader": "5.2.4",
"css-minimizer-webpack-plugin": "3.0.2",
"mini-css-extract-plugin": "2.1.0",
"normalize.css": "8.0.1",
"sass": "1.32.11",
"sass-loader": "11.0.1",
"style-loader": "2.0.0",
"webpack": "5.36.1",
"webpack-cli": "4.6.0",
"webpack-dev-server": "3.11.2"
}
}