Say I run a Node script from /foo/bar, though the script itself is located somewhere else (e.g. node_modules). Is there a way to get the working directory where the script was called from, i.e. /foo/bar?
What you want is process.cwd(), which returns the current working directory of the Node.js process.
Related
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.
I.e. 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.
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.
You can use path.join(__dirname,yourpath);
I'm new to WSL and Linux, but I'm trying to follow installation instructions for rhasspy (https://rhasspy.readthedocs.io/en/latest/installation/#windows-subsystem-for-linux-wsl). I have run the make install command successfully and the next step says I should copy rhasspy somewhere in my path but I can't quite figure out what copying to path means.
When installation is finished, copy rhasspy.sh somewhere in your PATH and rename it to rhasspy.
I added it to path but nothing changed so I was wondering if there is something I'm doing wrong. Right now when I run rhasspy on wsl it says rhasspy.sh: command not found. Any help would be really appreciated!
What it says is, put it in some place where the system will look for it when you type its name without full path in the shell.
There is an environment variable PATH that contains all those locations, separated by a :. (Check out echo $PATH.)
So, the author of these instructions leaves it up to you whether...
You want to copy the file to a location of your choice that is already in the PATH, such as /usr/local/bin or ~/bin.
Usually ~/bin is a good choice because it is per-user and doesn't pollute the system.
(Note that the directory ~/bin is added to the PATH by your .profile file only if it exists, so if you don't have this directory yet and create it now, you need to start a new login shell or run . ~/.profile1 before you can use it.)
- OR -
You want to create a new directory specifically for this application (say for example ~/opt/rhasspy) and append that directory to the PATH variable.
This can be done by adding the line export PATH=$PATH:~/opt/rhasspy to your ~/.profile file. Then, start a new login shell or reload the file using . ~/.profile1 for the changes to take effect.
If the directory in which this file is currently located is OK for you to keep permanently, then you can also just add that directory to the PATH instead of creating a new one.
Note: The PATH always contains directory paths in which the shell will look for executable files. It does not contain the actual file paths!
1: Yes, technically it is "cleaner" to log into a new shell or to run that one export statement manually instead of using . ~/.profile because the latter will apply things a second time that were already done before, so for example it can end up with the same directory in the PATH multiple times in the current session. In most cases that is fine though.
PATH is an environment variable. When you launch env, you see the list of known environment variables on your system.
In order to add something to your PATH variable, you need to take the variable, add the mentioned directory (preceeded by a semi-colon, most probably, as a separator) and store this again as the PATH variable. This can be done as follows (own example):
export PATH=$PATH:/home/this_user
the "PATH" it is referring to in linux is just inside the folder called /usr/bin. when you type a command into the terminal it looks for a program with that name inside the location. im not sure if this is the PATH you are looking for but hope it helps
I am working on a remote Linux server, connected using SSH. I am new to Unix.
My repo is stored in root/data/anj/myrepo. Now my home directory root/home/anj/ contains a bash file which executes the code stored in myrepo. But when I run the bash file, it doesn't seem to recognize the root directory and reach the repo. I get the output "file not found" when it tries to execute a file in my repo. How do I get the bash script to recognize the path from the root directory to the repo? It seems that the file is interpreting /data/anj/myrepo as root/home/anj/data/anj/myrepo, which shouldn't be happening.
You have to put a / in front of root, because if you don't do that then you are looking for a relative path, but i guess you are looking for the absolute path. so try /root/data/anj/myrepo instead of root/data/anj/myrepo :D
Good luck!
How would I get the directory from which the user invoked my script? I need to be able to resolve relative paths from command line arguments, among other things. Node provides process.argv, where process.argv[0] is the location of NodeJS, and process.argv[1] is the location of the script, but I don't see a way to get the place from which it was called.
Run: process.cwd() to get the directory from where the script got called.
I'm reloading my script a lot and would like to change the default directory where node repl is looking at so that I don't have to provide the whole path.
Is that possible?
The node.js REPL uses the current working directory (the directory in which you invoked the REPL) as a base when you use relative paths.