简体   繁体   中英

Why is node.js not running external js files?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM