{{- $options := (dict "includePaths" (slice "node_modules")) -}} {{- $style := resources.Get "sass/main.scss" | resources.ToCSS $options -}} main.scss @import "leaflet/dist/leaflet"; Tested, works fine. Answer from jmooring on discourse.gohugo.io
🌐
Sass
sass-lang.com › documentation › at-rules › import
Sass: @import
All Sass implementations allow users to provide load paths: paths on the filesystem that Sass will look in when resolving imports. For example, if you pass node_modules/susy/sass as a load path, you can use @import "susy" to load node_modules/susy/sass/susy.scss.
Discussions

How to reference a scss files installed to node_modules?
I am trying to import a scss file from node_modules. This to be exact: https://github.com/material-components/material-components-web-react/tree/master/packages/button I tried @import '@materia... More on github.com
🌐 github.com
20
July 26, 2018
How to import css file from node_modules?
I’ve tried to import css file from node_modules like this: @import '../node_modules/tailwindcss/base.css'; @import '../node_modules/tailwindcss/components.css'; @import '../node_modules/tailwindcss/utilities.css'; howe… More on discuss.frappe.io
🌐 discuss.frappe.io
0
0
March 1, 2023
Import a SCSS files from Node Modules
Look at adding scripts/styles to angular.json. Something like the above should generally be added there instead of importing it. Edit: the above was for bootstrap where you want it globally. For the other one it says in the error that the file doesn’t exist, I’d check your import path and spelling to make sure you’re pulling the proper scss file. More on reddit.com
🌐 r/Angular2
2
1
July 25, 2024
node.js - Importing Sass through npm - Stack Overflow
None of the systems above describe ... "main" css file of a package is. How does (i.e. sass-npm) infer this? 2016-05-11T14:10:11.15Z+00:00 ... Important: the whole node_modules folder contains so much, just set it launch extremely slow in watch mode. Your should only set your package paths, eg: $npx sass -w --load-path=node_modules/foo --load-path=node_modules/bar/scss ... From offical ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 15
459

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.

2 of 15
297

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:

  1. 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";

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

🌐
GitHub
github.com › parcel-bundler › parcel › issues › 1800
How to reference a scss files installed to node_modules? · Issue #1800 · parcel-bundler/parcel
July 26, 2018 - /Users/alex/Developer/sasm2/src/app.scss: File to import not found or unreadable: ./@material/react-button/index.scss. at options.error (/Users/alex/Developer/sasm2/node_modules/node-sass/lib/index.js:291:26) I also tried @import '../node_modules/@material/react-button/index.scss'; which results in
Author   athei
🌐
Frappe
discuss.frappe.io › t › how-to-import-css-file-from-node-modules › 101820
How to import css file from node_modules? - Frappe Forum
March 1, 2023 - I’ve tried to import css file from node_modules like this: @import '../node_modules/tailwindcss/base.css'; @import '../node_modules/tailwindcss/components.css'; @import '../node_modules/tailwindcss/utilities.css'; howe…
🌐
Reddit
reddit.com › r/angular2 › import a scss files from node modules
r/Angular2 on Reddit: Import a SCSS files from Node Modules
July 25, 2024 -

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",
Find elsewhere
🌐
Ruby on Rails
discuss.rubyonrails.org › t › how-do-css-in-node-modules-get-loaded › 78111
How do *.css in node_modules get loaded? - Ruby on Rails Discussions
May 31, 2021 - I’m not supposed to import them in application.js am I? I’d expect them to load automatically within the context of their module
🌐
Bobby Hadz
bobbyhadz.com › blog › react-import-css-from-node-modules
Import a CSS file from node_modules in React | bobbyhadz
April 6, 2024 - The example imports the CSS for the bootstrap package directly from the node_modules directory.
🌐
Medium
medium.com › anna-coding › how-to-import-scss-file-from-node-modules-abb09b6946b3
How to import scss file from node_modules | by Anna Coding | Anna Coding | Medium
February 19, 2020 - How to import scss file from node_modules To import scss file from node_modues, you need to config the path. Firstly, add .sassrc file under the root and config the path there. Anna Coding Free web …
🌐
Reddit
reddit.com › r/angular2 › angular library: how to import scss from node_modules
r/Angular2 on Reddit: Angular Library: How To Import SCSS from node_modules
April 27, 2021 -

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
npmjs.com › package › node-sass-package-importer
node-sass-package-importer - npm
@import '~bootstrap/scss/_alert.scss'). This module also supports partial file name resolving so you can import files by only specifying their base name without prefix and extension (e.g. @import '~bootstrap/scss/alert'). Sadly bootstrap and most other frameworks do not load their dependencies directly in the concerned files. So you have to load all dependencies of a file manually like in the example above. I recommend you to do better and to import dependencies directly in the files that are using them. In the example below you can see the default configuration options. cwd: Defines the path in which your node_modules directory is found.
      » npm install node-sass-package-importer
    
🌐
GitHub
github.com › Shopify › slate › issues › 582
How to import SCSS from node_modules? · Issue #582 · Shopify/slate
May 22, 2018 - But it was failed. I also tried to import scss from node_modules like this · @import '../../../node_modules/bootstrap/bootstrap'
Published   May 22, 2018
Author   awesomenkj
Top answer
1 of 2
7

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.

2 of 2
1

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.css via node_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"
  }
}