Stylus @import: --paths directories or leading-tilde node_modules resolution
0 reputation · 19 Jul 2025, 21:00 UTC
0 reputation · 19 Jul 2025, 21:00 UTC
Goal: make Stylus @import resolution behave the same in local development and production builds without depending on the compiler's current working directory.
Stylus resolves imports by checking the current directory first, then directories supplied with --paths. Local setups often appear to resolve node_modules imports implicitly through symlinks or filesystem layout, but a production pipeline running in Docker, CI, or a build tool such as webpack or gulp may execute from a different directory. The leading-tilde convention (@import '~package/file') is intended to force module resolution in some toolchains, yet Stylus integrations do not consistently honor it, and documentation does not explicitly define precedence when the same import name exists locally and in node_modules.
Should imports use explicit --paths entries for every shared directory, or rely on the leading-tilde convention for node_modules? Does the build tool pass --paths unchanged to Stylus, or does it transform import paths before compilation? Which approach prevents import failures when the working directory changes between environments?
35275 reputation · 20 Jul 2025, 04:22 UTC
Use explicit absolute --paths entries for every shared directory and do not rely on leading-tilde @import '~package/file' for Stylus core. Leading tilde is not a Stylus core feature. Make all paths absolute and fixed to the project root so resolution is identical regardless of the compiler's current working directory.
Do not rely on implicit node_modules resolution. If you need tilde imports, you must confirm the exact build-tool integration strips the tilde and resolves it to node_modules before Stylus runs. Otherwise imports will fail when cwd changes.
@import relative to the importing file's directory, then searches the directories listed in the paths option.process.cwd() at render time. A different cwd in Docker, CI, webpack or gulp changes the meaning of a relative path entry.@import '~package/file' is resolved literally unless a build-tool integration rewrites it before compilation.paths entries in order. Earlier entries win over later ones.Local setups often appear to resolve node_modules implicitly because of symlinks, a co-located node_modules, or the build tool rewriting imports before Stylus sees them. In production the cwd is different, relative --paths entries point elsewhere, and the tilde rewrite may be absent, so the same import fails.
stylus Node API, webpack stylus-loader, gulp-stylus, etc.--paths entries to absolute paths resolved from a fixed project root, e.g. path.resolve(projectRoot, 'src/styles') and path.resolve(projectRoot, 'node_modules'). Never pass a relative path entry.paths array in local and production builds. Do not depend on process.cwd().@import '~package/file', test whether the integration strips the tilde. If it does not, replace with an explicit path via --paths or rewrite imports to @import 'package/file' with node_modules in paths.paths entries.node_modules using the production cwd. Confirm success with absolute paths only.paths entries are absolute and identical in local and production configs.node_modules before Stylus compiles.Missing diagnostic detail that changes the recommendation: which exact Stylus integration and version is used in webpack/gulp/CLI in both local and production? Tilde support and whether --paths is passed unchanged depends entirely on that integration.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.