Custom importer for node-sass to import packages from the
const sass = ;const packageImporter = ;sass;
// Import the file that is specified in the `package.json` file of the module.// In the case of bootstrap, the following file is loaded://;
// Import only specific files:;;;;
The "~" is mandatory and marks the import path as module.
If only the module name is given (e.g.
@import '~bootstrap') the importer looks in the
package.json file of the module for the following keys: "sass", "scss", "style", "css", "main.sass", "main.scss", "main.style", "main.css" and "main". The first key that is found is used for resolving the path and importing the file into your sass code.
To load only a certain file from a module you can specify the file in the import url (e.g.
@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_modulesdirectory is found.
packageKeys: You can define the
package.jsonkeys to search for and in which order.
packagePrefix: You can set the special character for indicating a module resolution.
const sass = ;const packageImporter = ;const options =cwd: processpackageKeys:'sass''scss''style''css''main.sass''main.scss''main.style''main.css''main'packagePrefix: '~';sass;
node-sass --importer node_modules/node-sass-package-importer/dist/cli.js -o dist src/index.scss
Upgrade from 3.x.x to 5.x.x
prefixoption was renamed to
Why is there no 4.x version?
This module is maintained in one repository together with multiple other node-sass custom importers. The node-sass-magic-importer repository is using a monorepo approach with fixed versions for all packages. The projects maintained in the node-sass-magic-importer monorepo started out as separate repositories with separate versioning, so when they were integrated into the monorepo, the versions of all projects were raised to 5.0.0 and are in sync since then.
This module is powered by node-sass-magic-importer.