Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 51f9ad4

Browse filesBrowse files
zcbenzBethGriggs
authored andcommitted
src: add option to disable global search paths
PR-URL: #39754 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent 1ced732 commit 51f9ad4
Copy full SHA for 51f9ad4

File tree

Expand file treeCollapse file tree

7 files changed

+31
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+31
-5
lines changed
Open diff view settings
Collapse file

‎lib/internal/bootstrap/pre_execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js
+5-2Lines 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;
Collapse file

‎lib/internal/options.js‎

Copy file name to clipboardExpand all lines: lib/internal/options.js
+7-2Lines 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
};
Collapse file

‎src/env-inl.h‎

Copy file name to clipboardExpand all lines: src/env-inl.h
+4Lines 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
}
Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+1Lines 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;
Collapse file

‎src/node.h‎

Copy file name to clipboardExpand all lines: src/node.h
+6-1Lines 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

Collapse file

‎src/node_options.cc‎

Copy file name to clipboardExpand all lines: src/node_options.cc
+6Lines 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);
Collapse file

‎src/node_worker.cc‎

Copy file name to clipboardExpand all lines: src/node_worker.cc
+2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.