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-importerVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage by importing Bootstrap from `node_modules` using the `~` prefix and rendering Sass content with `node-sass`.
Update your importer configuration to use `packagePrefix` instead of `prefix`. E.g., `{ packagePrefix: '~' }`.Ensure your Node.js environment meets the minimum requirements, typically Node.js 10 or higher for recent versions of this package and `node-sass`.
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.
Review the documentation of the third-party Sass library and manually add `@import` statements for any required dependencies that aren't automatically resolved.
Ensure `importer: packageImporter()` is passed to `node-sass.render()` or `options.sassOptions.importer: packageImporter()` is set in your `sass-loader` configuration.
Add the `~` prefix (or your custom `packagePrefix`) before the module name in your `@import` statement, e.g., `@import '~bootstrap/scss/variables';`.
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.