From 133e37beb5048b42900e04bf3c60f34498011f24 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sat, 15 Aug 2026 08:56:28 +0000 Subject: [PATCH] module: add a read-only mode to the compile cache A compile cache generated ahead of time and shipped inside an application package should only ever be read: the package may be immutable or covered by an integrity check, and a cache directory that appears at run time would be a surprise. Add readOnly to module.enableCompileCache() and NODE_COMPILE_CACHE_READONLY=1: existing entries are loaded as before, nothing is serialized or persisted, flushCompileCache() is a no-op, and the cache directory is used as found rather than created, so enabling against a missing directory fails instead of making one. Signed-off-by: Shelley Vohr --- doc/api/cli.md | 9 +++ doc/api/module.md | 18 +++++ doc/node.1 | 4 ++ lib/internal/modules/helpers.js | 15 ++-- src/compile_cache.cc | 70 ++++++++++++++----- src/compile_cache.h | 19 ++++- src/env.cc | 10 ++- src/node_modules.cc | 5 +- .../test-compile-cache-api-readonly.js | 65 +++++++++++++++++ 9 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 test/parallel/test-compile-cache-api-readonly.js diff --git a/doc/api/cli.md b/doc/api/cli.md index e213d055b8e9..b413791317e5 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3729,6 +3729,15 @@ Enable the [module compile cache][] for the Node.js instance. See the documentat When set to 1, the [module compile cache][] can be reused across different directory locations as long as the module layout relative to the cache directory remains the same. +### `NODE_COMPILE_CACHE_READONLY=1` + + + +When set to 1, the [module compile cache][] only reads existing entries from +its directory: nothing is written to it and it is not created if missing. + ### `NODE_DEBUG=module[,…]`