Cannot run nodejs script in command prompt - javascript

When I try to run a node.js program in Windows command prompt by stating its location, it will invariably say:
[stated location] is not recognized as an internal or external command, operable program or batch file.
In all answers to similar questions, in all node.js manuals, it is assumed you can just run a node.js file by calling it from its location. There will always be the suggestion of trying some hello world example BEFORE establishing a server and so on.
Even if I clean the command prompt with prompt $ cmd, and then write the whole location manually, I get the same message.
When I run
echo %path%
I get C:\Program Files\nodejs\bin
When I run
node -v
I get v6.10.3
When I run
node a00.js
(where a00.js is the script's name), it believes the whole path is a module, so it says it cannot recognize that module.
If I clear the command prompt with prompt $ cmd and then run node a00.js, it believes a00.js to be a module, so it says it cannot recognize that module.

Your path knows where node.js is, but it does not know where a00.js is. So you need to run the command as node followed by the path to file. (copy all commands including the double quotes)
node "C:\Program Files\nodejs\a00.js"
As an example, try this.
create a file called hello.js save it in C:\Windows\Temp\ (or where you prefer)
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
now open cmd and run it like this (assuming you saved it in C:\Windows\Temp\
node "c:\Windows\Temp\hello.js"
if you run it from path, meaning you CD to the directory where the a00.js file exists, then only can you run it as `node a00.js
as an example, assuming a00.js exists in C:\Windows\Temp:
cd c:\Windows\Temp
node a00.js
Important note when using any path, always enclose it in double quotes.
This will cause errors:
node C:\Program Files\test\a00.js
This will work:
node "C:\Program Files\test\a00.js"

I had a similar problem when node.js was complaining module does not exist , that is because it lets you save with a file name for eg If statement.js however when you call it in commander it does not like it so try changing the file name and run it and that could fix similar issues

Related

How to Change Default Directory in Node.JS

I am trying to change the default folder in node.js. I went to the following link in the node.js documentation:
https://nodejs.org/api/process.html#process_process_chdir_directory
I then generated the following code in a .js file:
console.log('Starting directory: ${process.cwd()}');
try {
process.chdir('C:\Users\HalvorSD\node-party');
console.log('New directory: ${process.cwd()}');
} catch (err) {
console.error('chdir: ${err}');
}
I get the error thrown in my console. The directory does exist so that's not the problem. Is my directory formatting incorrect or what's my issue?
I am trying to change the default from C:/Windows/System32/ to what I have above. Any help would be much appreciated.
JavaScript uses \ for String escape sequences. Use \\ for a literal backslash:
process.chdir('C:\\Users\\HalvorSD\\node-party');
Alternatively use path.join for cross-platform paths:
const path = require('path')
process.chdir(path.join('C', 'Users', 'HalvorSD', 'node-party'));
If you are going to change default directory for "Node.js command prompt" every time, when you launch it, then (Windows case)
go the directory where NodeJS was installed
find file nodevars.bat
open it with editor as administrator
change the default path in the row which looks like
if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"
with your path. It could be for example
if "%CD%\"=="%~dp0" cd /d "c://MyDirectory/"
if you mean to change directory once when you launched "Node.js command prompt", then execute the following command in the Node.js command prompt:
cd c:/MyDirectory/

How to run server written in js with Node.js

I have installed node.js from here http://nodejs.org/ . in my windows8 machine. copied the example server code in my server.js file
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
then opened the node.js prompt and written node c:/node/server.js
but nothing happens.
I am a php developer just trying hands on it, any guidelines will really be helpful.
You don't need to go in node.js prompt, you just need to use standard command promt and write
node c:/node/server.js
this also works:
node c:\node\server.js
and then in your browser:
http://localhost:1337
Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.
The command to run in the terminal will be
node server.js
But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.
I open a text editor, in my case I used Atom. Paste this code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
and save as
helloworld.js
in
c:\xampp\htdocs\myproject
directory.
Next I open node.js commamd prompt enter
cd c:\xampp\htdocs\myproject
next
node helloworld.js
next I open my chrome browser and I type
http://localhost:1337
and there it is.
Just go on that directory of your JS file from cmd and write node jsFile.js or even node jsFile; both will work fine.
Just try
node server
from cmd prompt in that directory
If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address. I am aware the newer Chrome OS versions no longer have this problem, but on my Chromebook, I still had to manually browse to the localhost's address for your code to work.
To browse to your locahost's address, type this in command line:
sudo ifconfig
and note the inet address under eth0.
Otherwise, as others have noted, simply type node.js filename and it will work as long as you point the browser to the proper address.
Hope this helps!

Where do I need to save javascript files for Node.js in windows 8

I have JUST downloaded node.js and am having to work with their command line for the first time. It appears that every tutorial on the planet gives the same starter app.
It uses the code...
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
and wants me to save it to the file example.js.
The file can then be run by typing...
node example.js
but all the command line gives me is
...
The tutorials do not say WHERE I should save the file. To my C drive? To the same file as node.js? Anywhere?
I have tried all three and they don't seem to work. If there is any other solution, or simply something else I should be looking into and asking about, that help would be appreciated too. But at this point I honestly have no idea what the problem is, and there appears to be very few resources to help me here.
When you run node example.js from your prompt, it's assuming that example.js is in the current working directory.
To change your current working directory, use the change directory (cd) command:
cd C:\Projects\MyProject
node example.js
This is equivalent to
node C:\Projects\MyProject\example.js
Correct! you can save it any where and change to that directory by the (cd) command on node.js command prompt which will then give message says: "Server running at: 127.0.0.1:8124.
So point your browser to that address and you see your file.

Starting a node.js server

I recently got into node and I installed it on my localhost. I am using WAMP. I am on Windows Vista.
Anwyay, I installed it. I made a new file in my localhost directory with this being called server.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
response.end('Hello World\n');
}).listen(1337);
then I went to node and tried typing % node server.js and all I got was an ellipses. What gives?
UPDATE: I checked my Systems variable and saw that my PATH lists the node.js as C:\Program Files (x86)\nodejs\
Run cmd and then run node server.js. In your example, you are trying to use the REPL to run your command, which is not going to work. The ellipsis is node.js expecting more tokens before closing the current scope (you can type code in and run it on the fly here)
The NodeJS code that you wrote is corrected, and it should work but as #dwerner said you have to write node server.js in the command prompt not inside the Node REPL.
But today most of who work with NodeJS are developing using a development environment (IDE). By using IDE you get a hotkey for running your code, and many things that can help you in the daily development (Syntax highlight for e.g.)
One of the most popular IDE's today for NodeJS development is VSCode, you can check it out.
As dwerner and aminadav mentioned, you need to run the node command for the main .js file you're using for your script/app. This file will typically be index.js by default, when you run npm init to create the package.json for your NodeJS project.
Maybe you will find this blog post that covers the basics helpful as well. :)
https://dev.to/bishopwm/my-first-server-and-rest-api-essentials-for-frontenders-2gnk

problem in running node.js

I am new to node.js. I followed the download and installation instruction from Here.
Problem is now when I try this in my terminal
node
I get following message
No script was specified.
Usage: node [options] script.js [arguments]
Options:
-v, --version print node's version
--debug[=port] enable remote debugging via given TCP port
without stopping the execution
--debug-brk[=port] as above, but break in script.js and
wait for remote debugger to connect
--v8-options print v8 command line options
--vars print various compiled-in variables
Enviromental variables:
NODE_PATH ':'-separated list of directories
prefixed to the module search path,
require.paths.
NODE_DEBUG Print additional debugging output.
Documentation can be found at http://nodejs.org/api.html or with 'man node'
After that I tried this example :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
It says : ReferenceError: console is not defined.
I googled this and found that i havent installed nodejs properly so I installed it again but no success :(
Please help !!
The version in the installation manual is too old.
Do this
wget http://nodejs.org/dist/node-v0.4.6.tar.gz
gunzip node-v0.4.6.tar.gz
tar -xf node-v0.4.6.tar
cd node
./configure
make
sudo make install
BTW, here are official installation instructions: https://github.com/joyent/node/wiki/Installation

Categories