NodeJS: Get user's current working directory - node.js

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.

Related

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.
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);

Copying shell file to path

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

Where is node repl default directory and how to change it?

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.

Why does node.js path.resolve the empty string to the current working directory?

According to the docs, node.js's path.resolve function converts the passed argument to an absolute path. However, when I pass it the non-existent file '', it returns the current working directory:
~$ node -v
v0.8.14
~$ node
> require('path').resolve('')
'/Users/perimosocordiae'
> require('fs').statSync('')
Error: ENOENT, no such file or directory ''
Is this intended behavior? Are there any other cases in which a "resolved" path will exist when the input path doesn't, or vice versa?
Other parts of the documentation say:
If after using all from paths still no absolute path is found, the current working directory is used as well.
and
the different paths don't need to exist and may also be files.
path.resolve can be thought of, as the docs, say, as a series of cd commands--e.g., what would happen if I started at from (or process.cwd() if no from is specified, as in your example) and manipulated that path with the string in to.

Node: How to get the dirname where a script was called from?

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.

Resources