Registry / devops / node-sass-package-importer

node-sass-package-importer

JSON →
library5.3.3jsnpmunverified

node-sass-package-importer is a custom importer for node-sass designed to streamline the process of importing Sass files from `node_modules` directories within your Sass code. It simplifies path management by allowing module imports using a `~` prefix, such as `@import '~bootstrap';`, eliminating the need for complex relative paths. The package currently maintains a stable version of 5.3.3 and is part of a larger `node-sass-magic-importer` monorepo. It offers flexible configuration options, including customizable `packageKeys` to define which entries in a `package.json` file should be considered for import resolution (e.g., 'sass', 'scss', 'main'), and a configurable `packagePrefix`. Key differentiators include its robust path resolving logic for both root modules and specific files within modules, support for multi-level `node_modules` directories (since v5.2.0), and the ability to glob import empty directories (since v5.1.0). Recent updates address TypeScript resolution and shared context issues in Webpack. While `node-sass` itself is in maintenance, this package provides crucial functionality for projects still relying on it, offering a consistent and manageable way to handle third-party Sass dependencies.

npm install node-sass-package-importer
INSTALL
IMPORT
SIG · NODE-SASS-PACKAGE-
N
node-sass-package-importer
devopsjavascriptv5.3.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

packageImporter
const packageImporter = require('node-sass-package-importer');
CommonJS import for use directly with `node-sass` in Node.js environments.
packageImporter
import packageImporter from 'node-sass-package-importer';
import { packageImporter } from 'node-sass-package-importer';
ESM import for TypeScript or modern JavaScript bundler setups. The package exports a default function.
ImporterOptions
import type { ImporterOptions } from 'node-sass-package-importer';
import { ImporterOptions } from 'node-sass-package-importer';
TypeScript type import for configuring the importer options.

Demonstrates basic usage by importing Bootstrap from `node_modules` using the `~` prefix and rendering Sass content with `node-sass`.

import * as sass from 'node-sass'; import packageImporter from 'node-sass-package-importer'; const scssContent = ` @import '~bootstrap'; @import '~bootstrap/scss/variables'; body { background-color: lightgray; } `; sass.render({ data: scssContent, importer: packageImporter(), includePaths: [], // Add relevant include paths if needed outputStyle: 'expanded' }, (error, result) => { if (error) { console.error('Sass compilation error:', error.message); return; } console.log('Compiled CSS:\n', result.css.toString()); // Example of using options: const customOptionsImporter = packageImporter({ packagePrefix: '@', packageKeys: ['style', 'sass', 'main'] }); console.log('Importer with custom options created.'); });
Debug
Known issues
breakingThe `prefix` option for configuring the package importer was renamed to `packagePrefix` in version 5.x.x. Old configurations using `prefix` will no longer work.
fix
Update your importer configuration to use `packagePrefix` instead of `prefix`. E.g., `{ packagePrefix: '~' }`.
affects: >=5.0.0
breakingVersion 5.3.0 dropped support for older Node.js versions. Projects running on very old Node.js environments might experience compatibility issues or failures.
fix
Ensure your Node.js environment meets the minimum requirements, typically Node.js 10 or higher for recent versions of this package and `node-sass`.
affects: >=5.3.0
gotchaThe underlying `node-sass` library is officially deprecated and is in a maintenance state. While `node-sass-package-importer` continues to function, users are encouraged to migrate to `sass` (Dart Sass) for future-proof projects, which has its own module resolution mechanisms.
fix
For new projects or major refactors, consider switching to `sass` (Dart Sass) and its built-in `@use` and `@forward` rules or its custom importer API, which is different from `node-sass`'s API.
affects: >=3.0.0
gotchaIf you are importing third-party libraries that have internal dependencies not specified in their main files, you might need to manually import those dependencies in your Sass files.
fix
Review the documentation of the third-party Sass library and manually add `@import` statements for any required dependencies that aren't automatically resolved.
affects: >=3.0.0
Errors
Common errors & fixes
Error: Can't find stylesheet to import: ~bootstrap
The `importer` option was not correctly configured in `node-sass.render()` or `webpack.sass-loader` options.
fix
Ensure `importer: packageImporter()` is passed to `node-sass.render()` or `options.sassOptions.importer: packageImporter()` is set in your `sass-loader` configuration.
Error: Can't find stylesheet to import: bootstrap/scss/variables
The module import prefix (default `~`) was omitted when attempting to import a package from `node_modules`.
fix
Add the `~` prefix (or your custom `packagePrefix`) before the module name in your `@import` statement, e.g., `@import '~bootstrap/scss/variables';`.
TypeScript: Cannot find module 'node-sass-package-importer' or its corresponding type declarations.
TypeScript might be having trouble resolving the package's types, possibly due to an older version of the package or incorrect `tsconfig.json` settings.
fix
Upgrade to `node-sass-package-importer` v5.3.2 or later, as it includes a fix for TypeScript resolution. Also, ensure your `tsconfig.json` includes `node_modules` in `typeRoots` or `include` paths.
Upgrade
Version history
5.3.3latest on npm
Audit
Dependencies
node-sassrequiredRuntime dependency for the custom importer function which integrates with `node-sass`'s rendering pipeline.
Agent activity
4 hits · last 30 days
node
4
Resources
node-sass-package-importer — npm install node-sass-package-importer · libregistry