node.js "Command not found" - javascript

I am attempting to work with node and learning how to write in Javascript. I am currently trying to use my terminal to run a basic "Hello world" js file using node. However, when I attempt to use $node index.js (that's what the file is called). All the terminal responds with is index.js command not found. I tried to do it with the just the terminal window, I have also tried to run it through the integrated terminal within data studio, so I am unsure of where I went wrong.
Here is a screen shot of my window.

Remove the $ sign. In web examples, docs use $ to show that you run it in the command line.
Instead use node index.js, or set up a package.json file and use npm start (npm start should contain node index.js).
Final Clarification (if you didn't understand the above explanation)
Run node index.js. $ is never used, except to show you that the command is used in the command line.

Related

Execute Javascript using Node in terminal

I know that
Node.js allows to write JavaScript code that runs directly in a
computer process itself instead of in a browser
Reading this, I try to open my terminal and run:
console.log('hello')
but I get zsh: unknown file attribute.
The same happens with node console.log('hello').
If I instead create a file hello.js and put the console inside it and then I run:
node src/hello.js
it works. Why? What am I missing?
Sorry for the (maybe) stupid question
It's because the console.log('hello') is being evaluated by your shell (zsh) before running the command and it has no idea what console.log('hello') is. You could use the -e or --eval arguments.
$ node -e "console.log('hello')"
If your using linux, first type node It will profile for you a REPL. which is essecially the excution terminal.
You can also do the same on windows

Can't run a javascript file through node.js

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\

Create file with command line in Node

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)

Trying to create a global npm module. When executing my command I get "The system cannot find the path specified"

I'm trying to convert a npm module I created to be used globally. https://github.com/toymachiner62/node-mongo-seeds/tree/global
When I run $ seed, it gives the error The system cannot find the path specified so it knows that it's a global command.
I'm not sure where to start debugging this. I have console.logs in all my scripts and none of them are firing so the problem lies elsewhere, but i'm not sure what to try next.
Advice?
edit
I'm now on my mac and I changed the shebang path to node in seed to #!/usr/local/bin node.
I still get these errors when trying to use my module:
$ seed
-bash: /usr/local/bin/seed: /usr/local/bin: bad interpreter: Permission denied
$ sudo seed
sudo: unable to execute /usr/local/bin/seed: Permission denied
edit
It seems this is only an issue on one of my macs. The other one works just fine..
You should name the script bin/seed.js and call it like that. Also, the path to node seems incorrect at the top of bin/seed.

Install Node.js

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

Categories

Resources