I am newbie in Node.js and started making hello world exercise. I have run into a problem, it is that I make a file with code 'console.log ("Hello World");' and saves it as a test. Then I open it in cmd and write node and then the node test, but for this answer ..... do not know why it does it, although I expect Hello World? I've tried and looking at the internet but can not find an answer to it.
I have install git and node.js.
hope someone can help me?
For example, if you save your file in `/home/toto/example.js with :
console.log('Hello World');
You just launch that by :
nodejs /home/toto/example.js
# or
cd /home/toto/
nodejs example.js
If the command nodejs is not found, test with the command node. It depend of your installation package.
node /home/toto/example.js
# or
cd /home/toto/
node example.js
1.Save the helloword.js file into a directory of your choice for example the folder "home"
2.Go to the directory in command prompt where the "helloword.js" file is by with cd:
cd home
3.You can test if the file is there by typing:
ls (mac)
dir (windows)
4.If the file is there simply write
node helloworld.js
and you should be good!:)
Related
I have been trying to figure out how to open a new terminal window from a Node application and run a preset command or script there.
I can open the terminal window:
const { exec } = require('child_process')
exec('open -a Terminal ' + process.env.HOME)
And this works fine, I have done a bit of research but it seems I can only find examples directed to bash
Could anyone help me on this?
Thanks
One way to do this would be to place the commands you want to run in a shell script with the .command extension. By default, these files will be opened in the Terminal application on macOS. This is a bit of a workaround, but I think it achieves what you would like to do.
Say we have the file hello.js containing the following:
console.log("Hello World")
And the shell script run_node.command
#!/bin/bash
node hello.js
Running the command open run_node.command will open a new Terminal.app instance and run the shell script, which in turn runs the node command. The terminal will close when the script exits.
I am trying to run a hello world program written in javascript in a separate file named hello.js
Currently running windows version of node.js.
The code runs perfectly in console window but how do I reference the path in windows environment.
C:\abc\zyx\hello.js
in Unix I guess it is showing $ node hello.js
I'm absolutely new to Node.js Please correct me if I am doing something wrong.
I tried
> node C:\abc\zyx\hello.js ----didn't work
> C:\abc\zyx\hello.js --didn't work
UPDATE1:
Added node.exe to the folder where hello.js file is sitting.
Added path point to the folder c:\abc\zyx\ and I get an error that says
ReferenceError: hello is not defined
see contents of hello.js
setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');
UPDATE 2:
So far I have tried all these version and none of them seems to work. May be I am doing something completely wrong.
>node hello.js
>$ node hello.js
>node.exe hello.js
>node /hello.js
>node \hello.js
> \node \hello.js
> /node /hello.js
> C:\abc\xyz\node.exe C:\abc\xyz\hello.js
> C:\abc\xyz\node.exe C:/abc/xyz/hello.js
> hello.js
> /hello.js
> \hello.js
>node hello
Refer to my file structure
.
├── hello.js
├── node.exe
└── paths.txt
RESOLVED:
Instead of running node.exe, try running in command prompt with the following option and it worked.
c:\>node c:\abc\hello.js
Hello
World! (after 2 secs)
Here are the exact steps I just took to run the "Hello World" example found at http://nodejs.org/. This is a quick and dirty example. For a permanent installation you'd want to store the executable in a more reasonable place than the root directory and update your PATH to include its location.
Download the Windows executable here: http://nodejs.org/#download
Copy the file to C:\
Create C:\hello.js
Paste in the following content:
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/');
Save the file
Start -> Run... -> cmd
c:
C:>node hello.js
Server running at http://127.0.0.1:1337/
That's it. This was done on Windows XP.
Install the MSI file:
Go to the installed directory C:\Program Files\nodejs from command prompt n
C:\>cd C:\Program Files\nodejs enter..
node helloworld.js
output:
Hello World
You need to make sure that node is in your PATH. To set up your path, this out.
Make sure that the directory that has node.exe is in your PATH. Then you should be able to
run node path_to_js_file.js.
For a good "Hello World" example, check out: http://howtonode.org/hello-node
another simple way
download nodejs to your system
open a notepad write js command "console.log('Hello World');"
save the file as hello.js preferably same location as nodejs
open command prompt navigate to the location where the nodejs is located
c:\program files\nodejs
and run the command from the location like c:\program files\nodejs>node hello.js
in case the js file in another location give the path of file
c:\program files\nodejs>node path\hello.js
I installed node for windows.
There is a node.js command prompt when I search for node.js in windows 7 start menu
If you run this special command prompt, you can node anything in any location without setting up the path or copy node.exe everywhere.
WinXp:
I have created a .bat file
node c:\path\to\file\my_program.js
That just run my_program.bat from Explorer or in cmd window
Go to cmd and type: node "C:\Path\To\File\Sample.js"
Windows/CMD does not know where the node file is located. You can manually type out:
path=%path%;"c:\Program Files\nodejs"
each time you open a new cmd.exe prompte
OR (in Windows 10),
right click on This PC -> properties.
Click on Advanced system settings -> Environment Variables (bottom right).
Select Path and click Edit.
Click new and enter C:\Program Files\nodejs.
Reboot and you should be able to run node from any directory.
Just change file association of .js file to node.exe and you can run directly from explorer.
Right click on the file -> Select "Open with" -> Select "Choose another program"
Check box "Always use this app to open .js file"
Click "More apps" -> "Look for another app in PC"
Navigate to node.js installation directory.(Default C:\Program Files\nodejs\node.exe"
Click "Open" and you can just see cmd flashing
Now you will be able to run any .js files directly just by double clicking.
Note: Adding below code to the end of js file will be useful as it will keep the console open for a keystroke.
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
The problem was that you opened the Node.js repl while everyone automatically assumed you were in the command prompt. For what it's worth you can run a javascript file from the repl with the .load command. For example:
.load c:/users/username/documents/script.js
The same command can also be used in the command prompt if you first start node inside the command prompt by entering node with no arguments (assuming node is in PATH).
I find it fascinating that 1)everyone assumed you were in the command prompt rather than repl, 2)no one seems to know about .load, and 3)this has 273 upvotes, proving that a lot of other node.js beginners are similarly confused.
Step For Windows
press the ctrl + r.then type cmd and hit enter.
now command prompt will be open.
after the type cd filepath of file. ex(cd C:\Users\user\Desktop\ )
then hit the enter.
please check if npm installed or not using this command node -v.
then if you installed will get node version.
type the command on command prompt like this node filename.js . example(node app.js)
C:\Users\user\Desktop>node app.js
type node js command prompt in start screen. and use it.
OR
set PATH of node in environment variable.
Install node properly, that's it.
Now open terminal to path where your .js file is.
for example- if Javascript file(fileName.js) is at folder C:\Documents\Project1.
Just go to that folder, type node to check node is enable
then type node fileName.js
Simple! Your work is done!!
c:\> node.exe %CD%\hello.js
%CD% captures the current directory under DOS
For all stuck on how to start!
https://github.com/sethvincent/javascripting
Copy here incase link dies:
Open node.js command prompt
Make directory called javascripting by typing "mkdir javascripting"
Change directory into the javascripting folder by typing "cd javascripting"
Create a file named introduction.js by typing "touch introduction.js" OR FOR WINDOWS: "NUL > introduction.js"
Open the file and type some javascript e.g. "Console.log('hello');"
Save the file and check it runs by typing "javascripting verify introduction.js"
All you have to do is right click the .js file on Windows and press "Open with Command Prompt"
OR
Open cmd, copy the path to the folder containing your script, and run the command "cd [paste text here]". Then do "node example.js"
I had such problem for windows. And I decided it so:
startApp.cmd:
#set JAVA_HOME=C:\jdk160_24
#set PATH=%JAVA_HOME%/bin;%PATH%
#cd /D C:\app\
#start /b C:\WINDOWS\system32\cscript.exe
C:\app\start.js
and saved it cmd file in dir C:\scripts
next file is runApp.bat:
C:\scripts\startApp.cmd
I'm trying to figure out node.js and nothing is working. I can run code using node.js via writing it in the terminal, but I want to run an external js file.
Right now, I have my node.js installed here "C:\Program Files\nodejs\node.exe"
I then made a js file and wrote console.log("Hello World"); and saved it in the same location and called it test.js.
I then opened up a command prompt and tried everything to try run it but nothing works.
The PowerPoint my teacher gave to me says to do "node test.js" and it should output Hello World.
When I try it, it says error cannot find test.js, so I wrote the full file path and it says, c:\program is undefined or something. Can someone please tell me what to write in the command prompt so I can output Hello World via the test.js file I've got saved in the same location as node.exe?
I am using windows if that helps.
You need to put test.js your current directory. If you just open CMD, your current directory defaults to your user directory. Instead of putting your js file next to the node.exe, you have to put it in the directory your cmd is in.
Example:
create test.js in C:\Users\yourname\Documents\test.js
then open CMD and cd C:\Users\yourname\Documents\ to set your CMD's current directory to your documents folder.
From there, node test.js will run the test.js file you have in C:\Users\yourname\Documents\
So I'm attempting to do this Node.js tutorial, and it says to create three .js files from the command line.
touch server.js client.js test.js
Except I get the following error:
'touch' is not recognized as an internal or external command, operable
program or batch file.
Not sure what is wrong here. I've installed Node.js as well as npm and browserify. I've created the package.json file correctly.
I suppose I could go into the project directory, right click and make a new file that way, but that defeats the purpose doesn't it?
What is the actual command to create a new file in the command line?
I'm using Windows 7.
That command is for a unix environment. You can use the following to create an empty file with similar functionalities that touch has in windows:
echo $null >> server.js in the desired directory
You can use the following command:
echo> index.js
touch is generally present on *nix platforms but not Windows. You will be able to follow the tutorial without it.
The tutorial author is using it to create empty files. You could achieve the same by simply saving files with the same names using an editor.
Alternatively, if you really want to use it in Windows, try Cygwin.
I know this is an old post, but the question is still relevant and there is a way to integrate the touch command into Windows, using the touch-for-windows npm package. It can be installed globally via node/npm with npm install -g touch-for-windows.
As someone who uses a pretty customized terminal across multiple machines, Cygwin didn't provide some of the other features I often use. The echo command (and accepted answer) provided by ltalhouarne works, but I felt was cumbersome to use. This npm package, though old, operates exactly in Windows as the touch command in *nix.
You can use :
copy nul > Gulpfile.js
You can also use the following command:
copy con [filename.extension]
[Note: Don't include square brackets]
That's it!
Follow the link
For installation in Windows
https://www.npmjs.com/package/touch-for-windows
Installation
In command prompt type:
npm install -g touch-for-windows.
Usage
After installing, you can run this application via the command line, as shown below.
C:\pythonapp>touch index.html
Successfully created 'index.html'
Worked for me
cd > filename.txt works too... if u create txt files, then it will have the file path on them. Just remember to delete them.
If you want a cross-platform solution in an environment where you have Node.js installed, you can as well run javascript code with node:
node -e "require('fs').writeFileSync('server.js', '')"
node -e "require('fs').writeFileSync('client.js', '')"
node -e "require('fs').writeFileSync('test.js', '')"
The commands above will create your 3 files with no content. You can also replace the '' by any content you would like to have in the file.
For more complex logics, you can move your code into a javascript file and execute it like:
node path/to/script.js
If you use git, there is already a bash installed inside
c:\Program Files\Git\bin\bash.exe
You can use it to work without installing cygwin. It contains the touch command.
Use the below command example to create any file in cmd:
type NUL > index.js
(here index.js is the file I want to create)
I'm completely new to node.js. By reading the documentation and tutorials, I managed to download and install node.js on my windows.
How do I test it and make it work?
The file test.js is saved in the same directory as node.exe and contains:
console.log('Hello World');
Openning the command-line I typed:
$ node test.js
But nothing hapenns, just:
...
You are typing node test.js in the Node REPL not the command line. The ... is indicating that you haven't reached the end of a valid statement yet (because you are writing shell and not JavaScript).
Run a command line with your terminal emulator of choice (probably Windows Powershell if you are using Windows).
Run the Node REPL by executing node via the command-line without any arguments. The reason you're not getting the expected results is probably because you're running node.exe directly. Since you're using windows, start up CMD and run node.exe from there. Once you have the REPL running, try node test.js again, and this time it will work.
![enter image description here][1]I have run it in nodejs shell and it is simply working
> console.log('Hello World');
Hello World
undefined
>
Also I opened nodejs command prompt not default command prompt of windows and then
node path_to_file
it is working... same output