Skip to content

Latest commit

 

History

History
139 lines (103 loc) · 3.75 KB

README.md

File metadata and controls

139 lines (103 loc) · 3.75 KB

Node.js development template

Nix flake template for Node.js projects. It provides a development shell with Node.js, Corepack, Bun, Deno, and the ni package manager wrapper, plus git hooks and a flake formatter. The flake is built with purr and git-hooks.nix.

This repository is part of the develop-templates collection.

Quick start

Create a project from the template:

nix flake init -t "github:nixcafe/develop-templates#node" --refresh

To use a shorter registry name:

nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#node

If you use cattery-modules, the beans registry entry already exists.

You can also create a GitHub repository from this template:

gh repo create my-node-project --template nixcafe-develop/node --clone

Initialize git if needed, enter the development shell, and set up the project:

git init && git add -A
direnv allow

# Create package.json if the project does not have one yet.
npm init -y

# Install dependencies.
ni

# Add a dependency.
ni express

ni reads the lockfile to choose npm, pnpm, yarn, Bun, or Deno. If the project has no lockfile, it asks which package manager to use.

What's included

Tool Purpose
node Node.js runtime from pkgs.nodejs
corepack Package manager shim for pnpm and yarn
ni, nr, nlx, nun, nci Commands from @antfu/ni
bun Bun runtime and bundler
deno Deno runtime
nixfmt-tree Nix formatter exposed via nix fmt
deadnix Dead Nix code linter enabled in git hooks
statix Nix static analysis linter enabled in git hooks

The shell adds node_modules/.bin to PATH when that directory exists. It also sets COREPACK_ENABLE_DOWNLOAD_PROMPT=0 and prints the Node.js version when the shell starts.

Customization

Pre-commit hooks

nixfmt, deadnix, and statix are enabled by default in develop/checks/git-hooks/default.nix. JavaScript and TypeScript hooks (biome, denofmt, denolint, eslint, prettier) are included as commented examples that you can uncomment:

hooks = {
  nixfmt.enable = true;
  deadnix.enable = true;
  statix.enable = true;

  biome.enable = true;
  # denofmt.enable = true;
  # denolint.enable = true;
  # eslint.enable = true;
  # prettier.enable = true;
};

See git-hooks.nix for the full hook list. statix.toml holds the statix settings.

Pin a specific Node.js version

purr loads overlays from develop/overlays/ and applies them to pkgs. Create develop/overlays/node/default.nix:

final: _prev: {
  nodejs = final.nodejs_22;
}

The shell uses pkgs.nodejs, so the version printed at startup also changes.

Add system packages

Edit develop/shells/default/default.nix and add entries to packages:

packages = with pkgs; [
  node
  corepack
  ni # @antfu/ni
  bun
  deno
  ffmpeg
  openssl
];

Layout

.
├── flake.nix
├── .envrc
├── .gitignore
├── statix.toml
└── develop/
    ├── shells/
    │   └── default/
    │       └── default.nix
    └── checks/
        └── git-hooks/
            └── default.nix

Useful commands

direnv allow        # load the development shell
nix develop         # load the shell without direnv
nix fmt             # format Nix files with nixfmt-tree
nix flake check     # run the flake checks, including the git hooks
ni                  # install dependencies
ni express          # add a dependency