macos_exclude_node_module_stores

This script will exclude all node_modules and .pnpm-store directories in the given directory ($PWD if not given) and its subdirectories from Time Machine backups and prevent their Spotlight indexing.

Copy the script into your Node.js project and add it as a preinstall life cycle script to your package.json file:

package.json
{
...
  "scripts": {
    "preinstall": "scripts/macos_exclude_node_module_stores.sh"
  }
}

For this to work, you should not ignore pre- or post-scripts:

NPM

For security reasons, you might want to disable them globally via:

$ npm config --global set ignore-scripts true

Use --ignore-scripts=false to enable the pre- or post-scripts for a specific NPM command:

$ npm install --ignore-scripts=false

You can show your current configuration via:

$ npm config ls -l
pnpm

For security reasons, you might want to disable them globally via:

$ pnpm config --global set ignore-scripts true

Use --ignore-scripts=false to enable the pre- or post-scripts for a specific pnpm command:

$ pnpm install --ignore-scripts=false

You can show your current configuration via:

$ pnpm config list
yarn

For security reasons, you might want to disable them globally:

$ yarn config set --home enableScripts false

Use --ignore-scripts=false to enable the pre- or post-scripts for a specific pnpm command:

$ yarn install --ignore-scripts=false

You can show your current configuration via:

$ yarn config current

Alternatively, you can use this script as a custom script in your package.json file:

package.json
{
...
  "scripts": {
    "macos:exclude-node-modules-store": "scripts/macos_exclude_node_module_stores.sh"
  }
}

And execute it manually:

$ node --run macos:exclude-node-module-stores

Usage

$ scripts/nodejs/macos_exclude_node_module_stores.sh
$ scripts/nodejs/macos_exclude_node_module_stores.sh /tmp/example_project

$ xattr -r /tmp/example_workspace
$ tree --noreport -a /tmp/example_workspace
/tmp/example_workspace
├── project1
│   └── node_modules
└── project2
    └── frontend
        ├── .pnpm-store
        └── node_modules
$ scripts/nodejs/macos_exclude_node_module_stores.sh /tmp/example_workspace
$ xattr -r /tmp/example_workspace                                         (1)
/tmp/example_workspace/project1/node_modules: com.apple.metadata:com_apple_backup_excludeItem
/tmp/example_workspace/project2/frontend/.pnpm-store: com.apple.metadata:com_apple_backup_excludeItem
/tmp/example_workspace/project2/frontend/node_modules: com.apple.metadata:com_apple_backup_excludeItem
$ tree --noreport -a /tmp/example_workspace
/tmp/example_workspace
├── project1
│   └── node_modules
│       └── .metadata_never_index                                         (2)
└── project2
    └── frontend
        ├── .pnpm-store
        │   └── .metadata_never_index                                     (2)
        └── node_modules
            └── .metadata_never_index                                     (2)
1 node_modules and .pnpm-store directories are not backed up by Time Machine
2 Spotlight indexing prevented