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.
Answer from ProllyGeek on Stack OverflowIf 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
How to reference a scss files installed to node_modules?
Sass @use (NOT @import) with node_modules paths
node.js - Compile SASS and @import from node_modules - Stack Overflow
gulp - Importing SASS partials from node_modules - Stack Overflow
» npm install sass-import-modules
I'm using Webpack and Dart Sass, and it seems that the traditional ~ (tilde) shortcut to node_modules does not work with \@use. I have to specify all my node_modules paths in long relative syntax:
@use "../../../node_modules/animate.css/animate.css";
This is annoying. Is there no better way?
Your question is similar to this: Sass import not crawling node_modules to find appropriate package
You can include paths by passing the includePaths argument to gulp sass. e.g
.pipe($.sass({
includePaths: ['node_modules/bootstrap/scss/', 'another/path']
})
You can include node_modules in sass pathes:
.pipe(sass({
includePaths: ['node_modules']
})
Then you can import library's sass like this:
@import "bootstrap/scss/bootstrap";
Or, in case of material-components-web:
@import "@material/top-app-bar/mdc-top-app-bar";
This way:
- you don't need to add each lib to path
- the sub-dependencies imports will work seamlessly, e.g. mdc-top-app-bar in MDC example imports "@material/elevation/mixins".
» npm install node-sass-package-importer
» npm install node-sass-import
» npm install sass-module-importer
As per the SASS Docs, ~ will point to src/ folder, when we import any SCSS files. We need to tell angular to include node_modules/ path, when we import any sass file. This can be achieved using stylePreprocessorOptions property inside angular.json.
"styles": [
...
],
"stylePreprocessorOptions": {
"includePaths": [
"../node_modules/ngx-toastr"
]
}
As per latest angular 18, I faced same problem, I fixed it by simply removing the ~ symbol
Code which shows error
@import '@angular/material/prebuilt-themes/indigo-pink.css';
@import '~bootstrap/dist/css/bootstrap.min.css';
@import '~perfect-scrollbar/css/perfect-scrollbar.css';
What is the error
[1] Could not resolve "~bootstrap/dist/css/bootstrap.min.css"
What info given after the error
[1] You can mark the path "~bootstrap/dist/css/bootstrap.min.css" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.
Fixed code
@import '@angular/material/prebuilt-themes/indigo-pink.css';
@import 'bootstrap/dist/css/bootstrap.min.css';
@import 'perfect-scrollbar/css/perfect-scrollbar.css';
Regarding ngx-toastr/toastr.css it does not declare files in that package.json, this cannot be imported in your css or scss, therefore it should be imported via angular.json
"styles": [
"src/styles.scss",
"node_modules/ngx-toastr/toastr.css"
]