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

时间:2015-08-20 03:22:29

标签: javascript node.js environment-variables

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

  

要跟踪原始呼叫站点,请设置NODE_DEBUG环境变量:

设置fs env变量的示例:

$ 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

但其他设置方法不起作用?

1 个答案:

答案 0 :(得分:3)

env运行具有特定环境的程序。如果没有传递一个,它会列出环境变量;它不能修改其父进程的enivronment。要设置当前会话的环境变量,请使用export

export NODE_DEBUG=fs
node script.js

Shells会让你省略exportenv,但是:

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