Skip to content

Improve fnm module - error handling, user configuration, and missing version workflow#1220

Merged
fdncred merged 2 commits intonushell:mainfrom
fredrik-stock:fnm-improvements
Feb 9, 2026
Merged

Improve fnm module - error handling, user configuration, and missing version workflow#1220
fdncred merged 2 commits intonushell:mainfrom
fredrik-stock:fnm-improvements

Conversation

@fredrik-stock
Copy link
Contributor

Summary

Rewrites the fnm integration to be more robust, configurable, and helpful when things go wrong — particularly when a project requires a Node.js version that isn't installed yet.

I started writing this before i found this module in the nu_scripts repo, and I was already pretty far along when i found it. I stole what was good in the original and kept the particulars that I needed from my thing.

What changed

Robustness

  • fnm env --json is wrapped in try/catch so a broken or misconfigured fnm doesn't crash shell startup.
  • Early-return pattern replaces the top-level if not ... { } wrapper, improving readability.
  • PATH is deduplicated with | uniq to stay clean across shell reloads.
  • fnm use --silent-if-unchanged avoids redundant output when the version hasn't changed.

Missing version handling
The original hook ran fnm use and let its raw error print to the terminal with no follow-up. The new hook detects the failure and either:

  • auto-installs the missing version (if auto_install: true), or
  • prompts the user interactively, with a clean message and a [y/N] confirmation.

Install failures are caught and reported instead of silently ignored.

User configuration via $env.FNM_NU_CONFIG
All behavior is configurable without editing the script:

Option Default Description
triggers ['.nvmrc', '.node-version', 'package.json'] Files that trigger a version switch
auto_install false Install missing versions without prompting
install_flags [] Extra flags passed to fnm install
fallback_alias null Adds a stable fnm alias to PATH for non-shell consumers

Config is re-read on every directory change so updates take effect without restarting the shell.

Fallback alias for non-shell processes
Optionally places an fnm alias's bin directory (e.g. default) in PATH below the multishell path. The active fnm version always wins, but the fallback gives systemd services, IDEs, and other non-shell processes a stable path to node, pnpm, etc. (See README.md for why that's useful. I'd share my "nu env to systemd env" bridge too, but export def "yeet nu env vars into systemd"[] {...} tells you the level of polish in that one.)

Comments and documentation
The script is documented with inline comments explaining each section and the reasoning behind less obvious choices. README updated with configuration reference and examples.

Motivation

I don't even remember anymore, I started the day trying to get my backend to talk to my sql server, and five digressions later atleast my IDE sees pnpm. It's been a day. Frontend's running now atleast, and I figured I'd share back.

@fdncred fdncred merged commit f074325 into nushell:main Feb 9, 2026
1 check passed
@fdncred
Copy link
Contributor

fdncred commented Feb 9, 2026

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants