为什么设置了 NODE_DEBUG=fs 环境变量但不起作用?

Why NODE_DEBUG=fs environment variable is set but not working?

Node.js 关于 fs 模块的文档说 (https://nodejs.org/api/fs.html#fs_file_system):

To get a trace to the original call site, set the NODE_DEBUG environment variable:

以及设置 fs 环境变量的示例:

$ env NODE_DEBUG=fs node script.js
fs.js:66
    throw err;
          ^
Error: EISDIR, read
at rethrow (fs.js:61:21)
at maybeCallback (fs.js:79:42)
at Object.fs.readFile (fs.js:153:18)
at bad (/path/to/script.js:2:17)
at Object.<anonymous> (/path/to/script.js:5:1)
<etc.>

但是,如果我在命令行中预先设置 fs env 变量或在脚本中使用 process.env,变量已设置但不起作用,没有显示跟踪:

命令行中的前期:

$ env NODE_DEBUG=fs // set before running a script
$ node script.js // doesn't bring the trace

在脚本中:

'use strict';
process.env.NODE_DEBUG = 'fs'; // set it on the second line of a script

<...> // script goes here

我这两种情况结果如下,虽然设置了NODE_DEBUG=fs,但没有踪迹:

fs.js:81
      throw err;  // Forgot a callback but don't know where? Use NODE_DEBUG=fs
        ^
Error: EISDIR, read
    at Error (native)

问题是。 为什么命令行内联变量设置有效:

$ env NODE_DEBUG=fs node script.js,

但是其他设置方式不行?

env 在特定环境下运行程序。如果没有通过,它会列出环境变量;它不能修改其父进程的环境。要为当前会话 设置 环境变量,请使用 export:

export NODE_DEBUG=fs
node script.js

Shell 允许您省略 exportenv,但是:

NODE_DEBUG=fs node script.js # Like env; doesn’t create an exported NODE_DEBUG
NODE_DEBUG=fs
node script.js