Error getting when executing nodejs through command prompt - node.js

I have the below code in Nodejs and tried to execute the same through command line as well as in browser, but getting "unexpected identifier" error.
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('<b>Running in server...</b>');
}).listen('3000', '127.0.0.1');
File name created is module3.js
I am very new to nodejs. So can anyone please help me to resolve this. Thanks!!

If you like to run file, do it as usual for shell comman line (interpreter file):
D:\nodejs> node module3.js
If you like to work in nodejs command line, you need to load module by require and it will be loaded and evaluated:
D:\nodejs> node
> require('./module3')
If your module exports some function you may to call it:
D:\nodejs> node
> require('./module3')()
Check about exports here

Related

syntax error near unexpected token `(' in node js

I started setting up a node app but it prompt me this
syntax error near unexpected token `('
'/app.js: line 1: `var http = require("http");
this is my code for app.js
var http = require("http");
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
can anyone help me in this
This is probably because you are running your script as if it is a bash script.
You have two options
run your program with the command $ node your_pgm.js
add the line #!/path/to/node to the beginning of your program (it must be the very first line)
Why are you typing in bash?
Please try to type "$ node" first and then you should see:
Welcome to Node.js v14 (or any version that you have installed).

How does the nodejs exec() function loads a bash environment?

I recently stumbled upon an error where a process launched by the exec() function failed because it could not find a environment variable.
I found the solution here on SO but I now have a question.
Which bash environment does the exec() function from child_process loads ? (not sure if the question is well written so I'll explain a bit more).
// app.js
const {exec} = require('child_process');
const http = require('http');
// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
// Set a response type of plain text for the response
res.writeHead(200, {'Content-Type': 'text/plain'});
exec('echo $PATH > foo.txt'); // <<<<<<<<
// Send back a response and end the connection
res.end('Hello World!\n');
});
// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');
If I run the code above I get the foo.txt file with the content of $PATH inside, meaning the shell environment spawned by exec() can find the environment variables.
However if I try to change $PATH to $MY_OWN_VARIABLE it cannot find it even though I added in the .bashrc the line export MY_OWN_VARIABLE="foo bar baz".
(For testing purposes I am working in a VM as root so I changed the root bashrc).
It seems like the exec() is not using the bashrc while spawning a shell but it stills find some variables such as $PATH but not the ones I defined and I don't know why.
This is only pure curiosity, the solution linked above solved my issue but it's still bothering me.
Note: I am not familiar with node and probably not with how a bash shell is created.
Try to restart your root session and check if $MY_OWN_VARIABLE is set with echo $MY_OWN_VARIABLE. Probably just a session restart is needed.

Error settting up atom with nodejs on ubuntu

I am trying to set up nodejs with atom on ubuntu 16.10. I followed the steps given in this link. But when I edit the ~/.atom/config.cson file to
runner:
scopes:
js:”nodejs”
as given in the link, Atom gives an error
Unexpected new line after runner:
How do I get this correct?
EDIT
After using Dan Lowe's code, the atom error disappeared but the code doesnt compile.
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
It gives the error
./server.js: line 3: syntax error near unexpected token `('
./server.js: line 3: `var http = require('http');'
Is it not able to see nodejs?
If this is the actual config you are using, you have two problems.
The lines are not indented.
Around nodejs are smart-quotes, not normal double quotes. That is not valid syntax here.
You probably want this instead.
runner:
scopes:
js: "nodejs"

Cannot run node.js example. module.js:340 throw err cannot find module

When I type in node example.js to my command prompt it gives me an error message.
This is the code I'm using.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, 'localhost');
console.log('Server running at http://localhost:1337/');
I got that directly from the node.js website and it worked last night. It won't work today. Some help would be appreciated please.
Node.js looks for the file you pass it in the current directory.
If the file only exists in a different directory, it will not be able to find it.
You need to either save the file to the correct directory, or use cd to switch to the directory containing the file.

Why does Cloud9 IDE return the error "Script does not exist" when trying to run simple Node.js script?

I am attempting to create a simple Hello World Node.js script using Cloud9 IDE. My code is below and is the atypical 6 lines:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT);
I don't see any errors and the IDE is certainly not alerting me to any. I've ensured that the file is saved (even going so far as to commit it to my repo). Unfortunately, whenever I try to run the above code I receive the following error:
Error on server
Received following error from server:
"Script does not exist: server.js"
To illustrate the run configuration I've included the below screenshot:
I am pretty sure I am missing something obvious, but whatever it is its alluding me. What do I have misconfigured that could cause my server.js file to not run?
Your configuration is correct, there is however a temporarily problem that causes some of the VM's to not run a file. I've seen it before and it usually goes away after some time. Quick fixes now:
Either wait
Create a new project with the same file, chances are that it'll run
Your filepath doesn't look right. Shouldn't it be server.js and not /server.js? Try creating a new run configuration and set the file path to server.js.

Resources