Sass cannot find a stylesheet imported with a leading ~
File to import not found or unreadable: ~bootstrap/scss/bootstrapShort answer
The ~ prefix is not part of Sass. It is a convention of the webpack sass-loader, so a Sass compiler run directly has no idea what ~bootstrap means. Compile through sass-loader, or drop the ~ and give Sass a load path or the Node package importer so it can look inside node_modules.
Fix it fast
1Find out which cause you have
Find out who compiles the file. If it is webpack with sass-loader in the rules, go to the first cause; if it is a sass command or a Node script calling Sass, go to the second.
grep -n "sass-loader" webpack.config.js package.json grep -n "sass" package.json2Match what you see, then jump to its fix
- 1
The failing import starts with ~ and the build goes through the sass CLI, a Node script or an Express middlewareSass runs without webpack, so nobody resolves the ~Fix - 2
The project uses sass-loader but ~ imports still failThe webpack importer is switched off, or the ~ is used the old wayFix - 3
A Node script calls Sass and the import is not foundA load path would fix it but is not set in the JavaScript APIFix
- 1
What does the error look like?
File to import not found or unreadable: ~bootstrap/scss/bootstrap
Cause 1 of 3
Sass runs without webpack, so nobody resolves the ~
1Check
Sass resolves imports relative to the current file first and then through load paths. A ~ is only understood by the importer sass-loader adds on top. Without it, ~bootstrap is just a path that does not exist.
npx sass --version2Fix
Remove the ~ and let Sass find the package. On the command line add node_modules as a load path, or turn on the Node package importer (Dart Sass 1.71.0 and later) and use a pkg: URL.
# load path on the CLI
npx sass --load-path=node_modules style.scss style.css
// style.scss with the Node package importer
@use "pkg:bootstrap";
# CLI flag that enables the pkg: importer
npx sass --pkg-importer=node style.scss style.css3Verify
Run the compile command again. The stylesheet should be written without the import error.
npx sass --pkg-importer=node style.scss style.cssConfirmed by sass-lang.com
Cause 2 of 3
The webpack importer is switched off, or the ~ is used the old way
1Check
sass-loader passes every @use, @import and @forward request to webpack's resolver, which is what makes ~ work. Setting webpackImporter to false removes that. The docs also call the ~ deprecated and say it can be removed, because the loader tries a relative path first and then node_modules.
grep -n "webpackImporter" webpack.config.js2Fix
Remove webpackImporter: false (or supply your own importer), and write the import without the ~. Do not write ~/ because that means the home directory.
// before
@use "~bootstrap";
// after
@use "bootstrap";3Verify
Restart the webpack build and check that the stylesheet compiles.
npx webpack --mode developmentConfirmed by webpack.js.org
Cause 3 of 3
A load path would fix it but is not set in the JavaScript API
1Check
Sass looks through importers first and then loadPaths. A stylesheet is never searched in node_modules unless you add one of them.
grep -rn "loadPaths\|importers" build scripts 2>/dev/null2Fix
Pass loadPaths for plain directories, or add the Node package importer and import with pkg:.
const sass = require('sass');
const result = sass.compile('style.scss', {
importers: [new sass.NodePackageImporter()],
});3Verify
Run the script and confirm the output CSS contains Bootstrap rules.
node build-css.jsConfirmed by sass-lang.com
How do I stop it coming back?
- Write package imports without ~; the loader no longer needs it.
- Decide once how node_modules is found (load path or pkg: importer) and keep it in one build file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
Related fixes
Back end
Angular build fails with SassError: Undefined variable
SassError: Undefined variable.
Sass modules loaded with @use are scoped to the file that loads them and their variables need a namespace prefix.
4 causes, each with a fixBack end
npm install node-sass fails on a current Node.js version
node-sass fails to install on a recent Node.js version
node-sass is end-of-life and its newest release supports Node.js up to 20, so on newer Node.js the install falls back to a source build that fails.
3 causes, each with a fixBack end
webpack reports Module build failed from sass-loader
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
The sass-loader line only says which loader failed.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fix