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.
Related
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
I have an application where the output is written into a file (.py) by using javascript.
I'm using this application to write python script into the file. Now I want to run the python script automatically on cmd(Windows) right after the output was written.
Is there a way to do so ? Is it possible without using "NodeJS"
So apparently everything happens with a single click on the application.
Thanks.
Node js provides the child process module,
which you can use to basically spawn a child process from your js application.
since you have not shared any source code so i am not sure what your specific use case is but a basic way of spawning python script would be like this.
import { spawn } from 'child_process';
let scriptPath = './script.py' // path to your python script
var script = spawn('python', ['-u', scriptPath, arg1, arg2]); // arg1,arg2 can be any command line arguments required by your script or if not needed you can skip them.
script.stdout.on('data', data => {
console.log('Data: ', data.toString());
// implement your functionality here
});
you can similary bind on close and error events to your script and implement the functionality accordingly.
Why not storing your script in a script.py file? Why do you use .txt at all? With CMD and Python installed you should easily run .py scripts with a command line "python path/to/script.py", shouldn't you?
Edit: For checking out how to execute python on Node JS just use Google! Google is your friend! "execute python with node js" threw me this article: How to call python script from NodeJs
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");
I am fairly new to Node.js and I want to load a few javascript files in the browser but I can't seem to find anywhere how I can simply get libraries to be loaded inside the browser.
So, my question is, how would I be able to load javascript libraries client side with Node.js?
I'm fairly new to node as well, but I've been using browserify to deal with node packages. There are a quite a few node packages that won't work in a browser - if they will work in a browser they will usually say it in their description.
Browserify is really easy to use - you just require() a module just as you would when writing a non-browser script (as far as I can tell). When you're ready to test the script on a browser, you just run
browserify input.js -o output.js
and it pulls all of the dependencies from your require()'s into output.js, so then your require()s actually reference what you want it to.
--Updated for new comment--
As I see in your answer, you are talking about node.js packages? I am
talking about standalone random javascript libraries (so, for example
just one of my own js files that I want to load client side). Can you
also load random javascript libraries with browserify? (I googled a
bit but can't figure that out)
Yeah, you can do that; although I'm afraid I can't help you with the technicalities of it (like I said, new to it myself). Here's an example:
Bar.js (my library)
module.exports = bar; //tells node what to export in this file
var bar = {
message: function(msg) {
console.log(msg)
}
}
Foo.js (my script)
var bar = require('./bar');
bar.message("foo"); //logs "foo" to console
And keep in mind that require() refers to a file. So Bar.js would need to be in the same directory as Foo.js. If Bar.js was in a different folder, you would just do require('./folder/bar'). To tie it all together, you would run browserify on Foo.js, and it would automatically grab Bar.js' contents.
There are a lot of different ways you can define exports, so you'll need to google around to see exactly how to do it for how your lib is formatted. But that's the gist of it.
I have started implementing TDD in my JS project. I've implemented mocha for that purpose. As these are my first steps what I did:
Installed node.js
Installed mocha globally and locally to my project.
Wrote package.json setting dependencies.
Wrote makefile.
Wrote .gitignore to avoid uploading node_modules folder.
Folder structure
project
-- js
----filetotest.js
-- test
---- test.js
What I want to do is to run the command make test in order to run the tests inside test.js that tests the filetotest.js file.
I read about the node.js approach using exports. But is there some way to include the file in the test suite?
I'm stuck here, and I think that my doubt is more about the concept than the tech thing. Will appreciate a lot your help.
To clarify a little bit what I would like to do:
https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/
I would like to get a similar result through the command line.
Thanks so much,
Guillermo
You are doing it right.
Now export your function from filetotest.js, like this:
var f1 = function(params) {
// ...
}
exports.f1 = f1
In test.js, require this file
var f1 = require("./filetotest.js").f1
// test f1
Btw, if you will put your tests in /test directory, mocha will execute them automatically (given that it will be executed from the root of your project)