issue with node.js not executing properly - javascript

Okay so I am really new to server-side scripting but love to give it a try. My issue thus far is that when I attempt to launch a file like "hellonode.js" I cannot.
I launch node and attempt to access a file from within a folder called new
and I get this error:
console is undefined
however when I use node and manually type the address in I get the intended results
the javascript application works completely as intended
I really wanna know why it is I cannot execute Node from within a folder but if I manually go to it each time I can. It is rather frustrating

When you are going to execute a node script the 1st argument to node should be the uri of the script file. so
node path/to/your/nodeScript
path would be absolute or relative to your current working directory.
also you can run a node script by giving only the folder of the node script but you need to create the node script file as index.js
suppose you have a folder name MyFirstNodeScript and inside the folder there is a file named index.js the script would be
console.log('hello world!!!');
now you can run the script by node MyFirstNodeScript but you should be in the parent directory of the MyFirstNodeScript

I'm not sure how you get the error but essentially you just "opened" a .js file in windows, which resulted in windows JScript executing your file instead of node executing your file. Maybe because your node.js file, or do you simple double-click?
Basically if you want a file to double-click to start your server create a short .bat file that contains the working snippet to start your node script. However in reality you usually don't need a thing to double click at all.

I think you need to give the address to the current file in the directory. It's more of a command line way of executing files rather than a node js convention.
node .\hellonode.js
In Unix (Linux or Mac) command lines it should be like this:
node ./hellonode

Related

nodejs cmd dynamic directory navigation with sudo extension

Hi, right now I'm writing an electron program in which I execute a powershell file with:
sudo.exec('Powershell.exe -Command "D:\Directory\...\file.ps1"');
but since this will only work on my device I want to make the directory dynamic
like this:
sudo.exec('Powershell.exe -Command "../file.ps1"');
but for that to work I would need to know in which directory the cmd/powershell starts initially
or rather how I get it to start in the directory where the javascript file which runs this
is located (the file.ps1 isn't in this directory, but it would be easy to navigate from there)
Thx in advance

How can I run my node script using a shell script file

I am creating a personal site using node. It is going to help me keep track of the ebooks that I am reading.
At the moment, I need to cd to the project folder and run node server.js.
I thought I'd create a shell script and then I'd just have to double click on the file. The file would have the following code:
#!/usr/bin/env bash
node server.js
The first error I've got was that server.js is not found at the root directory, I fixed that by typing the full back, then it threw same error for all the dependencies in the application
(I have never used shell scripting)
Is what I am trying to do possible?
I am assuming you are Linux user.
Set the path in your $HOME/.profile
export MY_COOL_NODE_APP=$HOME/nodejs/app/cool_app
Now your script will run as:
node $MY_COOL_NODE_APP/server.js
So in case if you move your app, you will need to update your .profile

Bitnami Mean Stack

I have installed mean stack manager, but when I run command using node terminal console.log("Hello World") it works fine, but whenever I put Javascript sample file to any folder or anywhere in the mean stack install directory or install modules every it don't console the file, so I need your suggestions to where I have to put the Javascript file in mean stack manager to console the file.
Bitnami MEAN STACK MANAGER
Bitnami developer here.
The easiest way to play with node is using its interactive shell. Go to your installdir (in your case D:\New Programmes\New Server), double click on use_meanstack and execute:
$ node
Then, you should see > indicating that node is waiting for instructions.
You can also write all the instructions in a file with js extension (sample.js) execute it directly using:
$ node sample.js
You can place this file anywhere you want. By default, Meant Stack working directory is installdir, so you can place you file into D:\New Programmes\New Server to test it directly writing $ node sample.js. However, I think it would be a good pratice to place it under nodejs folder (in your case D:\New Programmes\New Server\nodejs). If you put sample.js there, you should execute
$ node nodejs\sample.js
I hope it helps

`fs.lstat` raises no error after I manually delete a file, but reading the file does

I want to submit to you this question:
If I delete a file manually (that is, not using fs.unlink in Node but using the GUI or a CLI command) after the server has already started, Node says that the file still exists when I use fs.lstat but if I try to read this file, Node throws ENOENT because the file does not exist.
I thought maybe the cause is that Node has an internal file list created when the server starts, and only when Node's filesystem functions are used to manipulates files and directories would this list be updated. Am I mistaken?
When you call fs.lstat node is going to request the information from the operating system. It is not going to fetch it from some internal cache.
This being said, it is quite possible to write a Node application that won't behave properly if files disappear after it is initialized. But this is not a fault in Node but a fault in the application itself.
I have found the bug!
At the server start, I create a folder.
When after the server start, I deleted this folder for a test (is a cache folder), I didn't remember that the file requested was in this folder and Node normally says "not found stupid"... ahahah sorry guys and thanks anyway

Running EXE from Firefox - in a certain path

I found the solution to run an executable file (.bat or .exe) from a local html page in Firefox - it is documented very well here
However, I need to start the executable in the folder where it is located. My executable is a .bat file (D:\Test\file.bat) and it contains the command:
echo %cd%
When I run it, it prints:
C:\Programs\FirefoxPortableLegacy36
instead of printing
D:\Test
Is there any way to specify in the htm/javascript where exactly (in which directory) to start my executable?
thanks.
So you want to make the working directory set to the directory where your batch script is? If this is the case then just add this line right in the beginning of your batch script:
pushd "%~dp0"
You can also read the commandline help for the for command. It explains the parameter replacement well.

Categories

Resources