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.
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 a Lerna monorepo with 2 modules(packages): ps and cli.
ps just exports a function whatever which cli imports.
When I try to debug this code using VSCode, however, my breakpoints stop on the generated Javascript files instead of my source Typescript files.
This has bothered me for hours and I have extensively played around with my tsconfig.json and launch.json as well as using vscode-pwa-analyzer to see that VSCode is able to detect my source TS code but I cannot figure out a fix.
EDIT: I am using the following setup:
macOS Big Sur v11.1
Node.js v14.8.0
Typescript v4.1.3
And here is a dump file of my above debug you can load to vscode-pwa-analyzer.
I can see here that I get some Unbound Breakpoint errors.
I replicated the problem, but I would say that this is the expected behavior.
Actually setting a breakpoint on a function declaration makes no sense.
If you change your whatever function as a sync function (i.e. function whatever(): void) you will see that VSCode doesn't let you to break on that line.
In the special case of an async function we know that the function body is actually wrapped in a Promise. Probably in this special case the typescript debugger let us to set a breakpoint on a function declaration line to let us check what happens outside the Promise wrap.
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.
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");
On our production server, I have minified javascript published and I'm not including a map file with it, because I don't want the user to be able to understand what's happening based on the error.
I have a logging service I've written to forward the angular exceptions (caught by $exceptionHandler) to myself via email. However, this stack trace is near unreadable:
n is not defined
at o (http://localhost:9000/build/app.min.js:1:3284)
at new NameController (http://localhost:9000/build/app.min.js:1:3412)
at e (http://localhost:9000/build/bower.min.js:44:193)
at Object.g.instantiate (http://localhost:9000/build/bower.min.js:44:310)
at b.$get (http://localhost:9000/build/bower.min.js:85:313)
at d.compile (http://localhost:9000/build/bower.min.js:321:23333)
at aa (http://localhost:9000/build/bower.min.js:78:90)
at K (http://localhost:9000/build/bower.min.js:67:39)
at g (http://localhost:9000/build/bower.min.js:59:410)
at http://localhost:9000/build/bower.min.js:58:480 <ui-view class="ng-scope">
What I'm wondering is: Is there a program where I can analyze this stack trace against the actual non-minified source code via map file (or not via map file if there's another way)
What you want to do is parse the source maps. This has nothing to do with web browsers. All you need to do is translate the minified reference into the unminified resource.
If you have any experience with NodeJS there is already a package that does this for you.
https://github.com/mozilla/source-map/
To install the library
npm install -g source-map
or
yarn global add source-map
Create a file named "issue.js"
fs = require('fs');
var sourceMap = require('source-map');
var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8"));
console.log(smc.originalPositionFor({line: 1, column: 3284}));
Run the file with node
node issue.js
It should output the location in the original file to the console for first line from the stack trace.
Note: I tell you install source-map globally for ease of use, but you could create a node project that does what you need and installs it locally.
I figured there was no super simple tool for converting a minified stack trace into a readable one using a source map (without having to use a web service), so I created a tool for it:
https://github.com/mifi/stacktracify
Install and use it as follows:
npm install -g stacktracify
Now copy a minified stacktrace to your clipboard - then run:
stacktracify /path/to/js.map
Adding to #Reactgular's answer, the below snippet will work with the latest version of source-map
const rawSourceMap = fs.readFileSync("./app.min.js.map","utf8");
const whatever = sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
console.log(consumer.originalPositionFor({
line: 1,
column: 3284
}));
});
And to add to the discussion on the thread a simple regex like /\/(\w*[-\.]?\w*).js:\d*:\d*/g
Below is a very simple regex to find all line numbers in a stacktrace.
//regex for patterns like utils.js, utils123.js, utils-name.js, utils.version.js
var patt = /\/(\w*[-\.]?\w*).js:\d*:\d*/g;
// returns matches like ['/app.min.js:1:3284', '/bower.min.js:44:193', ..]
var errorPositions = line.match(patt);
console.log(errorPositions);
if(!errorPositions || errorPositions.length === 0) {
console.log("No error line numbers detected in the file. Ensure your stack trace file is proper");
return;
}
errorPositions.forEach(function(error) {
findInSourceMap(error);
});
});
If you had access to the source map file externally and could get the same file structure you could work it out I guess, but I'm not aware of any tools outside the browser that will help you with that.
The added advantage of having the data in a running browser will allow checking of locals which you won't get even with a source map.
You might want to consider a tool such as rollbar to do error reporting. This will report all the locals in each frame to help debugging. It has support for sourcemaps outside the browser to address your security concerns.
Append comment directive for the JS running in the page.
//# sourceMappingURL=/path/to/your/sourcemap.map
In firefox (not sure about chrome) to tell the Debugger to use source maps if they are available, click the "Debugger settings" button and select "Show original sources" from the list of settings that pops up: