【发布时间】:2012-02-07 16:47:45
【问题描述】:
从节点手册中我看到我可以使用__dirname 获取文件的目录,但是从 REPL 中这似乎是未定义的。这是我的误解还是错误在哪里?
$ node
> console.log(__dirname)
ReferenceError: __dirname is not defined
at repl:1:14
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:320:10)
【问题讨论】:
-
__dirname和__filename也是 not available whennodeis called with the--experimental-modulesflag。 -
我在使用 eslint 时遇到了这个问题,我在 .eslintrc.json 文件中错误地设置了
"browser": true而不是"node": true。 -
如果这里没有任何效果,那就是 hack stackoverflow.com/a/49879107/696535
标签: node.js