简体   繁体   English

相对于文件位置运行脚本,而不是节点中的命令行位置

[英]Run script relative to file location, not command line location in node

I'm not sure exactly what to call what I'm trying to do, but I have a node script that accesses a folder (./commands relative to the script file), but when I run the script from somewhere other than the folder, it is unable to find./commands.我不确定到底该叫什么我正在尝试做的事情,但是我有一个节点脚本可以访问一个文件夹(./commands 相对于脚本文件),但是当我从文件夹以外的地方运行脚本时,它无法找到./commands。

Ie when I'm in the folder with the script in it, ./commands refers to \script-folder\commands, but when I'm on the desktop, the script looks in Desktop\commands and finds nothing.即,当我在包含脚本的文件夹中时,. ./commands指的是 \script-folder\commands,但是当我在桌面上时,脚本会在 Desktop\commands 中查找,但什么也找不到。

Is there any way to tell the script to run relative to its own folder, or do I just have to hard-code the full location of the commands folder in the script?有没有办法告诉脚本相对于它自己的文件夹运行,或者我只需要在脚本中硬编码命令文件夹的完整位置?

./ means current working directory. ./ 表示当前工作目录。 you can check with pwd command.您可以使用 pwd 命令进行检查。
You can use path.join(__dirname,yourpath);您可以使用path.join(__dirname,yourpath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM