elliott.david 2010-06-28 14:31 采纳率: 25%
浏览 333
已采纳

如何使用 Node.js 获得当前脚本的路径?

How would I get the path to the script in Node.js?

I know there's process.cwd, but that only refers to the directory where the script was called, not of the script itself. For instance, say I'm in /home/kyle/ and I run the following command:

node /home/kyle/some/dir/file.js

If I call process.cwd(), I get /home/kyle/, not /home/kyle/some/dir/. Is there a way to get that directory?

转载于:https://stackoverflow.com/questions/3133243/how-do-i-get-the-path-to-the-current-script-with-node-js

  • 写回答

13条回答 默认 最新

  • 撒拉嘿哟木头 2010-06-28 14:39
    关注

    I found it after looking through the documentation again. What I was looking for were the __filename and __dirname module-level variables.

    • __filename is the file name of the current module. This is the resolved absolute path of the current module file. (ex:/home/kyle/some/dir/file.js)
    • __dirname is the directory name of the current module. (ex:/home/kyle/some/dir)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料