Answer the question
In order to leave comments, you need to log in
How do I use Node to run a script that is NOT in the current directory?
Hey!
I wrote a simple script myscript.js that processes files of a certain format, and the file name will be passed as a parameter on the launch command line.
I put it in a directory specified in PATH (Windows) - like C:\SYS
Now, to set this script on any file, I write something like: node c:\sys\myscript.js datafile.txt
I'm a newbie to Node, all the subtleties I don't know yet and I have two questions:
1. How to get rid of the need to specify c:\sys\ in the script name? Set some environment variables, some node settings, something else.....
2. With this launch, the Node looks for the \node_modules\ folder in c:\sys\ or higher. How to tell her to look for her in the current folder or higher?
Those. ideally, I would like, being in the folder of the datafile.txt file , to type in com. line node myscript.js datafile.txt and that would work just fine.
Answer the question
In order to leave comments, you need to log in
Put in c:\sys batch file myscript.bat with the following content
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\myscript" %*
) ELSE (
node "%~dp0\myscript" %*
)
1. Is the script next to the file for reading? if so, then cd c:\sys\ and run node myscript datafile.txt
2. node searches for modules from the current run of the node itself to the root of the disk. those. just exits the folder up and looks for the modules folder and so on up to the root of the drive.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question