Why is node.js not running external js files? - javascript

I started the node.js part of my Web Development course, the introductory video shows the teacher creating new js and html files using the terminal, then runs the js file with node, I followed the steps and the js script ran, then on the next video, the teacher makes another js script with a console.log("Some hello message") and a second console.log(process.argv) I type this in VSCode, I try running it as I was taught: node fileName.js and the script doesn't runs, so I thought there was a typo or something was wrong with my script but it was not, and typing node filenName.js doesn't runs any script no matter the location, name or content of the script, I spent almost all day on this with no results, I just keep getting the same message Uncaught SyntaxError: Unexpected Identifier, and I get the same result if I type the specific location of the file instead of being in the folder with the js script, like this: node c/Users/user/Documents/fileName.js
I'm in the folder with js file I want to run, and I've tried placing the script in the root directory that my command line starts in and I've tried running scripts from other directories that are not the default, the result is the same message.
If I type .load fileName.js the script runs, it seems to be a specific problem with running the script with node fileName.js and I'm completely lost, I looked for answers here but I didn't found a solution for my specific problem, so I didn't felt like implementing any of the solutions for kind of similar problems since they involved changing default directories, and other measures that others said they didn't recommended because they could mess with administrator functions and honestly this is my first day using node, I'm not quite sure how to proceed here or if it's a small problem with an easy fix or if I have to remove and reinstall node which I read can be kind of a pain too since you have to scrub the system manually to get a fresh node install.[My node.js not running a simple js script][1]
Command line
ramaz#DESKTOP-28ABSA2 MINGW64 ~/Documents
$ node
Welcome to Node.js v14.16.0.
Type ".help" for more information.
> node firstScript.js
node firstScript.js
^^^^^^^^^^^
Uncaught SyntaxError: Unexpected identifier
>
JavaScript file
for (let i = 0; i < 10; i++) {
console.log("Hello From First Script!")
}
Conclusion: this was a non-issue, the teacher of my course just runs node fileName.js in the command line, he never goes in REPL, and I want to thank you guys for taking your time to reply to this.

I went ahead and created the same firstScript.js file and its contents in my Documents directory. I don't think re-installing node is necessary. I think the issue is simply a limitation with the node repl.
The moment you enter node into your terminal, you're entering Node REPL.it mode - a very good playground for testing JS code. Here's more info on this:
Unfortunately, on repl.it you cannot run any file except the original file. If you want to run another file on node.js, you can do this:
What I'm doing here is simply importing the file firstScript.js into my node REPL and executing it all in one line. Let me know if there are any more questions!

I just followed your instructions exactly.
I created a file named '''myfile.js''' that has in it
console.log("some hello message")
console.log(process.argv)
for (let i = 0; i < 10; i++) {
console.log("Hello From First Script!")
}
then, in the directory that has the file I type node myfile.js and it works fine.
it prints out
some hello message
[
'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\WilliamOHara\\subscripify-repositories\\myfile.js'
]
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Hello From First Script!
Getting started developing can be a little frustrating. Make sure that the contents of your file is exactly
console.log("some hello message")
console.log(process.argv)
for (let i = 0; i < 10; i++) {
console.log("Hello From First Script!")
}
you may have forgotten the parentheses around process.argv or left out the . between console and log

Related

NPM packages are not running with the node.js

Mysupername is the value that got printed out and not the expected output.
I already tried reinstalling it. I used npm install prompt -sync to use it with JavaScript inside Visual Studio Code but I don't know where I am going wrong. I have already added the variables in system advanced setting.
Visual Studio Code error message if I try to use prompt or alert inside and run through code runner extension:
There's a slight mistake with your code. Firstly, the reason why it is printing Mysupername to the console is because you provided console.log with a string. Instead just provide the variable name like this:
const superheroes = require('superheroes');
var Mysupername = superheroes.random();
console.log(Mysupername);
Now, the reason why prompt isn't working is because you never imported the library into your code, you can do it by the following:
const prompt = require("prompt");
Final note, alert is only available when running javascript through a browser.

Why is my code different in http-server than what I have in my file?

This is what my code looks like in my script.js file:
let defenseTable = document.querySelector('#home-stats');
When I run my code in localhost using http-server it gives an error saying defenseTable is not defined. When I go to debug the code line says
defenseTable = document.querySelector("#home-stats");
it removes my let variable declaration. Why? The code works fine by the way if I run it without http-server.
I suspect that you have http-server also configured with some other type of build step which is transpiling your code in some strange way, which is the only reasonable explanation for a variable declaration being dropped in this manner.

Need help getting started with Node and JS (learnyounode exercises)

I just started using node (also just learning javascript) and I am trying to do the learnyounode exercises (hello world, baby steps) and I have no idea how to even start.
(I installed node), I created a folder called "nodeexercises"
For the first exercise "Write a program that prints the text "HELLO WORLD" to the console". I created in my node exercises folder I created a file: "server.js"
In that file I wrote the code below
Then I saved it and run node server.js
-
var fs=require('fs');
fs.writeFileSync("hello.js","Hello World file");
var server=createServer(function(request,response){
response.end("Hello World");
});
server.listen(3000);
console.log("HELLO WORLD");
That wasn't right, however.
At that point, learnyounode is just showing you that node.js is acting like an interpreter.
Replace the code in your server.js file with:
console.log('Hello world!');
Then run like you did before. It's not much of an application, but it does Hello, World!
Checkout Web Development with Node and Express by Ethan Brown. It will get you a better understanding of how Node can be used for your own purposes.
Most of your code is redundant, no offense. To do 'Hello, World' in any javascript variant, including nodejs, you simply have to type make a file like yournamehere.js and place the line console.log("Hello, World!"); into it.
You can then navigate to the directory where the js file is and use the command node yournamehere.js and it should execute without a hitch. If it is not, something may be broken with your node.

Running server-side python in JS: difficulty with pico

I have webpage that needs to run some computation on start up. I want to keep this computation on the server side so the client cannot access the source code. I discovered pico, a module that is supposed to be "a bridge between server-side python and client side JavaScript".
I have a simply test.py:
import pico
def hello():
return "Hello World"
My JavaScript is also simple:
pico.load("../../../test.py");
pico.main = function() {
var displayMessage = function(message){
console.log("hello2");
console.log(message);
}
test.hello(displayMessage);
}
"../../../test.py" is the relative location of the python script to the pico folder
I then run "python -m pico.server" on the command line. When I go to my web page, open inspector, and go to the console I get the error: "Uncaught SyntaxError: Unexpected token i". 'i' is presumably from the first line import. Note that this same error happens if I don't run the pico.server command.
Any help would be great, as well as suggestions for alternative methods of doing this serverside vs clientside.
I may have an answer for you, however I have not been able to replicate the same error.
pico.load does not seem to work when file extensions are included in the argument, this is due to the function being designed to load sub-modules directly (i.e. module.sub_module) as in the pico API:
pico.load(module, [callback])
Load the Python module named module. The module will be available as a global >variable of the same name.
Submodules may be loaded by using dotted notation e.g. module.sub_module
To make sure I included ".py" file extension on the pico test page I have been working on and it failed to load the module, so this may be a problem if you are using the file extension.
Another possible issue was mentioned in a comment by holderweb. In the first pico example HTML the file client.js is included in an external <script> tag, this includes the functionality required to use pico. So you must have something similar to the following tag in your index.html head section:
<script type="text/javascript" src="/pico/client.js"></script>
For more insight I would be interested in seeing what/if the server logs at command line when the error occurs, and also the contents of your index.html page. Hope this helped!

How to obtain console.log output for Sublime Text 3 on Linux?

I can't see the output to console.log when I build JavaScript files using a custom Node.js build system in Sublime Text (build 3083) on Linux.
When I try to build jstest.js which contains just console.log("Hello world!"); Sublime Text's console reports Running /usr/bin/node /home/sophie/scripts/jstest.js and that the build is successful, but I do not see the expected "Hello world!" output.
If I execute node jstest.js in my Linux terminal, "Hello world!" is properly output, as expected.
I have node installed at /usr/bin/node. The file jstest.js is saved to disk (apparently, Sublime Text will not build unless the file is saved). I'm using the following custom build system (and it is selected before building, of course):
{
"cmd": ["/usr/bin/node", "$file"],
"selector": "*.js"
}
I've tried setting the location to just "node" and also removed the "selector" option, but neither had any effect, there's still no console.log output.
I've looked through a few similar questions and answers here (that's where I obtained the build system code), but nothing has solved the issue for me, yet. Any suggestions?
Apparently, the issue was a bug either with Sublime Text itself, or the Material Design (https://github.com/equinusocio/material-theme) theme I am using. To solve the issue, I did the following:
Switch to the default Sublime Text theme
Build the JS file using the custom Node.JS build system, confirm console.log output
Switch back to desired theme
Build the file again, console.log output should be visible
That worked for me, at least.
Try dropping the semi-colon at the end of your line of code, which would give you the following:- console.log("Hello Javascript World!") There is no semi-colon at the end of the code line, save the file and build. Be sure Node is selected as the build system.
An alternative would be to use: debug("output here");
This would replace console.log("output here");

Categories