"Cannot find module" after upgrading the Stylus CLI — safe recovery without breaking other projects
0 reputation · 14 Dec 2022, 23:12 UTC
0 reputation · 14 Dec 2022, 23:12 UTC
After upgrading the Stylus CLI, invoking stylus on an existing stylesheet fails with Error: Cannot find module, which appears to be a native binding or binary mismatch between the newly installed version and the current Node runtime. The goal is to get a working compiler back without disturbing other projects that may share the same global installation.
The uncertainty is which recovery path is actually safe: whether the global binary was overwritten in a way that affects unrelated projects, whether the failure is a Node version incompatibility rather than a corrupted install, and whether using npx stylus to resolve a locally installed version is a reliable way to bypass the broken global binary. There is also a concern that a partially failed upgrade could leave the compiler silently producing incorrect CSS instead of erroring, so any recovery needs a way to confirm output correctness, not just a clean exit.
Assuming a recent Stylus release on a current LTS Node version:
1. Does Error: Cannot find module after a CLI upgrade specifically indicate a missing native binding, and how should that be distinguished from a Node version mismatch?
2. Is resolving the compiler through npx stylus against a locally pinned version a documented-safe way to avoid global binary conflicts during recovery?
3. What is the minimal compile check that confirms the recovered binary produces correct CSS rather than silently wrong output?
30775 reputation · 15 Dec 2022, 11:05 UTC
To restore compiler functionality without affecting other projects, bypass the global binary and use npx to execute the compiler from a local project context. This ensures the runtime uses the modules pinned to that specific project's node_modules rather than a potentially corrupted global symlink.
# Run the local project version specifically
npx stylus [input-file].styl [output-file].css
If the project does not have Stylus installed locally, npx will fetch a temporary version. To avoid global conflicts entirely, it is recommended to install Stylus as a devDependency in every project: npm install --save-dev stylus.
The Error: Cannot find module message following a CLI upgrade typically indicates a pathing or symlink discrepancy rather than a native binary failure. In Node.js, a missing native binding (like a C++ addon) usually throws a more specific MODULE_NOT_FOUND or a binding.node error referencing a specific .node file.
A general "Cannot find module" error usually means the global shell command (the symlink in your bin folder) is pointing to a version of the package that was deleted or moved during the upgrade process, leaving the shell calling a ghost reference.
Using npx stylus is a documented-safe recovery method because it prioritizes the local node_modules/.bin directory. It does not modify the global installation, meaning other projects relying on the global binary remain untouched (even if they are also broken). It effectively isolates the execution environment to the current working directory.
To ensure the recovered binary is not silently producing incorrect CSS (a risk during partial upgrades), perform a variable-to-value check. Create a minimal test.styl file with a nested variable and a calculation:
$base-color = #ff0000
body
color = $base-color
margin = 10px * 2
Compile this and verify the output CSS contains exactly color: #ff0000; and margin: 20px;. If the output is empty or the variables are not resolved, the binary is corrupted and a full clean install is required:
npm uninstall -g stylus
npm cache clean --force
npm install -g stylus
To determine if this is a Node version mismatch rather than a corrupted install, please provide the output of node -v and which stylus (or where stylus on Windows).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.