Skip to content

Commit 51f9ad4

Browse files
zcbenzBethGriggs
authored andcommitted
src: add option to disable global search paths
PR-URL: #39754 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 1ced732 commit 51f9ad4

File tree

7 files changed

+31
-5
lines changed

7 files changed

+31
-5
lines changed

lib/internal/bootstrap/pre_execution.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const {
1111

1212
const {
1313
getOptionValue,
14-
shouldNotRegisterESMLoader
14+
noGlobalSearchPaths,
15+
shouldNotRegisterESMLoader,
1516
} = require('internal/options');
1617
const { reconnectZeroFillToggle } = require('internal/buffer');
1718

@@ -420,7 +421,9 @@ function initializeWASI() {
420421

421422
function initializeCJSLoader() {
422423
const CJSLoader = require('internal/modules/cjs/loader');
423-
CJSLoader.Module._initPaths();
424+
if (!noGlobalSearchPaths) {
425+
CJSLoader.Module._initPaths();
426+
}
424427
// TODO(joyeecheung): deprecate this in favor of a proper hook?
425428
CJSLoader.Module.runMain =
426429
require('internal/modules/run_main').executeUserEntryPoint;

lib/internal/options.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

3-
const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options');
3+
const {
4+
getOptions,
5+
noGlobalSearchPaths,
6+
shouldNotRegisterESMLoader,
7+
} = internalBinding('options');
48

59
let warnOnAllowUnauthorized = true;
610

@@ -57,5 +61,6 @@ module.exports = {
5761
},
5862
getOptionValue,
5963
getAllowUnauthorized,
60-
shouldNotRegisterESMLoader
64+
noGlobalSearchPaths,
65+
shouldNotRegisterESMLoader,
6166
};

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ inline bool Environment::hide_console_windows() const {
886886
return flags_ & EnvironmentFlags::kHideConsoleWindows;
887887
}
888888

889+
inline bool Environment::no_global_search_paths() const {
890+
return flags_ & EnvironmentFlags::kNoGlobalSearchPaths;
891+
}
892+
889893
bool Environment::filehandle_close_warning() const {
890894
return emit_filehandle_warning_;
891895
}

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ class Environment : public MemoryRetainer {
12031203
inline bool owns_inspector() const;
12041204
inline bool tracks_unmanaged_fds() const;
12051205
inline bool hide_console_windows() const;
1206+
inline bool no_global_search_paths() const;
12061207
inline uint64_t thread_id() const;
12071208
inline worker::Worker* worker_context() const;
12081209
Environment* worker_parent_env() const;

src/node.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,12 @@ enum Flags : uint64_t {
412412
// so that a worker thread can't load a native addon even if `execArgv`
413413
// is overwritten and `--no-addons` is not specified but was specified
414414
// for this Environment instance.
415-
kNoNativeAddons = 1 << 6
415+
kNoNativeAddons = 1 << 6,
416+
// Set this flag to disable searching modules from global paths like
417+
// $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
418+
// do not expect to have their behaviors changed because of globally
419+
// installed modules.
420+
kNoGlobalSearchPaths = 1 << 7
416421
};
417422
} // namespace EnvironmentFlags
418423

src/node_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,12 @@ void Initialize(Local<Object> target,
10731073
Boolean::New(isolate, env->should_not_register_esm_loader()))
10741074
.Check();
10751075

1076+
target
1077+
->Set(context,
1078+
FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"),
1079+
Boolean::New(isolate, env->no_global_search_paths()))
1080+
.Check();
1081+
10761082
Local<Object> types = Object::New(isolate);
10771083
NODE_DEFINE_CONSTANT(types, kNoOp);
10781084
NODE_DEFINE_CONSTANT(types, kV8Option);

src/node_worker.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
572572
worker->environment_flags_ |= EnvironmentFlags::kHideConsoleWindows;
573573
if (env->no_native_addons())
574574
worker->environment_flags_ |= EnvironmentFlags::kNoNativeAddons;
575+
if (env->no_global_search_paths())
576+
worker->environment_flags_ |= EnvironmentFlags::kNoGlobalSearchPaths;
575577
}
576578

577579
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)