Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshruti khandekar
119 Pointsnode does not allow variable reuse
While typing I made a mistake
const selenium = requie("selenium-webdriver"); ReferenceError: requie is not defined at repl:1:18 at ContextifyScript.Script.runInThisContext (vm.js:44:33) at REPLServer.defaultEval (repl.js:239:29)
Now I am trying to correct it but I get the following:
const selenium = require("selenium-webdriver"); SyntaxError: Identifier 'selenium' has already been declared
Online search suggested this as a common bug. How do I go about deleting a declared variable?
Thanks
4 Answers
andren
28,558 PointsYou have to reset the Node REPL by exiting it with the command .exit
and then entering the REPL again.
There is no way to delete a variable while inside the REPL session, and that is not a bug but the exact thing the const keyword is designed to do. It creates a constant, a value that cannot be reassigned. If you use the let keyword then Node won't complain about this.
Craig Dennis
Treehouse Teacherconst
will do that. let
will allow you to reassign variables.
Here's some more info: https://teamtreehouse.com/library/let-and-const
Hope that helps!
shruti khandekar
119 PointsThanks Craig. I have 4 more questions in the same. 1) Once I did make a mistake, do I just reopen a new cmd window and start from scratch or can I correct the mistake in the current cmd. I am using Windows. 2) Also is the node in command window just used for temporary scripting or does the code get saved some place? 3) If I write a large block of code in cmd using node and towards the end make a mistake similar to the const one above, will I need to start from scratch or can I continue correcting my mistake? If I can correct my mistake, how is it done?) Are you suggesting that using const is not a good idea for future and I should go with let instead? What if I want to use const but make a mistake? Thank you
shruti khandekar
119 PointsI think andren answered my first and third question.
shruti khandekar
119 PointsThank you so much andren.
shruti khandekar
119 Pointsshruti khandekar
119 PointsI think it is a bug since the assignment was faulty, so node should not have considered that as a valid assignment. I mean the bug should have been caught by compiler (or is there such a thing here since we are using runtime engine?)
andren
28,558 Pointsandren
28,558 PointsAh you are right, I didn't consider the fact that the assignment failed, sorry about that. I do agree that Node should not create a
const
if the value you assign to it is invalid or faulty.Also I see you posted some questions for Craig, while I'm not him I think I can answer most of them:
If you make a mistake with
const
then you do indeed need to start the whole Node session over from scratch, you can't undo that. That does not require you to close CMD though, only to exit and reenter Node like I discussed in my answer above.The code entered in the REPL is indeed temporary, it does not get stored anywhere. The REPL is only intended as a playground for you to test code, when you start building proper scripts you have to do that in a regular JavaScript file. This will be demonstrated in a later section of this course.
As mentioned above you can't really correct
const
issues, and some typos will straight up crash the REPL so often times you will have to start over when making a mistake.In the Node REPL I would actually recommend using
let
instead ofconst
, since it avoids the issue you have just run into. But when writing code in an actual script I recommend usingconst
, as in that case you don't really have to worry that much about typos as you restart the program everytime you run the script.