I am currently trying to catch up with some JS basics, before continuing with my node.js-project.
I wanted to execute the file and kept getting the error 'console is not defined', when I wanted to use console.log('Hello!')
The only helpful way to run the file normally was with: node ./myjsfile.js
I installed node.js for that node project.
But for just running super basic JavaScript I don't want node.js.
What are better (lighter!) ways to run my JavaScript files inside Visual Studio Code?
Like what's the most basic way to do it?
No, if you do not currently have Node, you will not have the 'play button' to run the code. So, if you go to Run in the toolbar at the top, you should see some options to run the file.
I'm pretty sure you can just press F1 and then Run Code
Related
I just started to learn Javascript. I decided the best thing to use was vscode, as I use it for other programming projects. But when I do even a simple statement which I know is right, such as "console.log('hello world')" it gives a error message. Something along the lines of file "c:\Program Files\ Python39\lib\run.py.py" , line 197, in _run_module_as_main. I have tried making a new JavaScript file, and install additional support for js. But It did not work. code:
console.log('hello world')
I think you should start learning by using chrome or firefox console.
Chrome Detail
Firefox Detail
If you want to run JS file on VSCode you can simply go ahead and install node on your machine, and use it run js code in the terminal. Just google it how to install and then use command node <fileName>.js and this will do the job. And in case you don't know how to open terminal in vscode just use command: ctrl + `
https://github.com/Valish/sherdog-api
After downloading it what I did was I first installed node.js, which I think I need in order to get everything for this (I have never used any of this stuff before at all). Then I went into command prompt and navigated to the place where the program im trying to use is. I then typed npm install and it installed all the files the program needs. I think that installed it correctly.
I then went into my code editor and tried to run the code through a browser. That didn't work because there is a function or something called "require" that I guess the browser doesn't have access to or anything.
Online, people seem to be saying that you run node.js programs through command prompt?
To test that I created a JavaScript file that has just this in it console.log('Hello');.
I then navigated to the file location and typed in command prompt "node hello.js" and it printed to the console so I know that works. What I don't know is it relevant that it works. This simple JavaScript file could have nothing to do with how the other program runs. I don't know.
Knowing that the simple file ran like this I did the same thing with the index.js file of the program I want. But when I do this nothing happens in the command prompt, it just brings up another command line. I can't figure it out.
On the GitHub site in the readme.md file for this program there is a "usage" section. However it doesn't explain how to use it as far as I can tell. It says
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072"
sherdog.getFighter(url, function(data) {
console.log(data);
})
If this is how you use it I don't know what to do with this, where to input it, or anything. There is no real documentation to go with it at all. I'm stuck at this point; I don't know where to learn or what to look at.
You need to type the sample code into a new .js file. sherdog-api is an api, which means it will not run any code by itself. Run your new .js file with node.
Node is a server running with JavaScript. It is mainly based on modules. A module just contains some code that helps you, most modules can't actually be run directly just like in your case. Instead you have to install the module, and then you can write code that works with it. At first make a new folder for your project, e.g. myCoolServer then open a command prompt in that folder and run:
npm init
To set up everything you need to run nodejs here, then run
npm install sherdog
To install the module. It will appear under /node-modules/. Now just create a new file like you did with hello.js, put the code into it:
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072";
sherdog.getFighter(url, function(data) {
console.log(data);
});
console.log("searching...");
And then run it using node thatfile.js, and you should see the data appearing after a while.
If you succeeded with that, you can continue here...
I am QA Engineer with very minimal developer experience. My skills are limited to writing automated tests and in my time I have always used Visual Studio Enterprise (with selenium webdriver) and TestComplete IDE's with their in built debugger. I have recently made a move to something new and want to get to grips with Nightwatch JS for my end to end tests. I have written some tests and they are running fine. However, I am now at a stage where a test is failing and I want to debug just that one test. How I would do this??? Usually in VS enterprise or TestComplete, I would just pop a break point on a line of code, right click on the one test script and it will pause at the break point.
For my Nightwatch JS project I have used Visual Studio Code. I know that I can run a single test by opening the terminal in VS Code and running the command "nightwatch tests/folder/path/myTest.js". There is not much help on nightwatch out there that has helped me and the only post I found on here was this one but it has not solved my problem. Because when I click the debugger "Play" button it starts to run ALL of my test not the one test I am interested in.
I have tried to play the debugger and then in the terminal run my cmd for the one test but the test will run through and does not hit my break points. Any guru's out there that can help?
Please ask away if you need me to post any information.
You can pass additional options for Nightwatch in your terminal, such as -- --tag.
So, you can tag each single test file you have, like this:
module.exports = {
'#tags': ['iamatag'],
'[EXAMPLE TEST]': (client) => {
code
code
code
}
};
Then, in your terminal, you would run:
$ npm test -- --tag iamatag
Only tests with the tag 'iamatag' will run.
Cheers.
Yes, I use F12 in the browser all the time to test out Javascript snippets with the console. I'm tired of that (for the moment anyway).
I've read that in Visual Studio you can use the immediate window to run Javascript interactively.. I've haven't tried it that hard. I think when I did it told me it can't evaluate while in design mode... ugh, what a pain.
I do like to use Visual Studio Code (sublime text historically) sometimes to just mess around with syntax of snippets. Would also be nice if I could just run Javascript there too quickly. Is there a package I could download in VSCode to do so? Or something already built in?
As of (at least) my current version of VS Code (1.5.2), the "Debug Console", while debugging, lets you run arbitrary JavaScript code as you would in the VS Immediate Window. (Similar to as you would for the Chrome Dev Tools Console.)
There is no Immediate Window unlike Visual Studio in VSCode. But you can still execute Javascript in VSCode.
Install the Code Runner Extension - https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
Open the JavaScript file in VSCode, then use shortcut Ctrl+Alt+N, the code will run and the output will be shown in the Output Window.
I've found this extension that makes a scrathpad for JS, that runs at the same time as you are typing: https://quokkajs.com/
Works on VS Code, Jet Brains, and Atom.
If you don't want to start a debugging session or installing an extension, a simple way to have a JavaScript console is to start Node in a Terminal.
View -> Terminal
Start node (without any argument -you'll need node in your PATH)
Now you have a repl with auto-complete and value preview.
It doesn't have the features of the Chrome Console, but I find it good enough for evaluating JS code while I'm working.
Run the command node in the terminal below the editor, this will create a node environment where arbitrary JavaScript can be entered. You must first have node (and apparently npm) installed from nodejs.
This might do it: https://code.visualstudio.com/Docs/runtimes/nodejs
Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js is the runtime and NPM is the Package Manager for Node.js modules.
To get started, install Node.js for your platform. The Node Package Manager is included in the Node distribution.
I want to run some javascript code (unit and interaction tests) right in visual studio and be able to see console output. Is it possible to do that with VS???
Node.js Tools for Visual Studio has a REPL for node.js that can be used for such things.
I love the Interactive Window for Visual Studio (and 2017) but lately I have been starting to use Visual Studio Code. I really like it but the Interactive Window (REPL) is missing.
I need that. So I tried several extensions but none of them was as good as Node.js Tools for VS. The solution was to make one my self, you may need NodeJs 8+ to use Node.js REPL for Visual Studio Code.
Preview of Node.js REPL for VS Code
I believe it is possible, at least to a degree. I haven't done exactly what you're saying, but I do have node running in iis with iisnode. I also know that there is a version of iisnode that runs under iis express. I would look into that.
My guess is you would be able to launch a project using the iisnode module, and you could set the default document to the .js file you want to run, but you may only be able to get console output through the browser.
iisnode does give you the ability to debug and see the console output through your browser. For example, if you run the iisnode module for a file test.js at localhost:1234/test.js, you can get a the browser developer tools by going to localhost:1234/test.js/debug. That would give you console output, but I'm not totally certain you would be able to get it directly in VS.