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 trialDennis C
5,010 PointsReferenceError: Token is not defined
I'm getting a 'ReferenceError: Token is not defined' when I'm running player.js. I realize it's because the Token.js isn't imprted into Player.js, yet it doesn't get addressed in a solution video. How do I import it?
Ryan Lertola
Full Stack JavaScript Techdegree Graduate 15,992 PointsI'm getting the same error and cannot figure out how to fix it.
Why does it work perfectly in the videos when she does not do anything to it? I tried doing the import/export fix above, but now I'm getting 'Unexpected Identifier' error. I right clicked on the Token.js file, and copied the path and imported that. Is that correct?
Thanks
J llama
12,631 PointsJeffrey Meesters wow shame on you for such a horrible response. what a waste of all of our lives
2 Answers
Geoffrey Neal
30,298 PointsHi Dennis,
I haven't done this course myself so not entirely sure of the context, but from the looks of it you're using the es6 javascript syntax. In order to import es6 modules from another file you can do so in a variety of ways, depending on how the class/function/whatever is being exported, here are 2 examples:
// Token.js
class Token {
// ...
}
export default Token
Or
// Token.js
export default class Token {
// ...
}
In this case the class is exported as the default, which means it is the main thing being exported from the file. In this case you can import it using the following syntax:
// Player.js
import Token from 'path/to/Token.js';
The other way to export something from an es6 js file is the following:
// Token.js
export class Token {
// ...
}
In this case you are omitting the default
keyword, and so this is not the main thing being exported. In this case you would refer to it like so:
// Player.js
import { Token } from 'path/to/Token.js';
This is a little over simplified as there are many ways to import and export es6 modules. You can find more information here.
Dwayne Bynum
3,069 PointsWhen I downloaded the project files I noticed that the scripts were out of order if you are not going to take the js module approach. Move the Token.js script above the Player.js script: Before
<script src = "js/Game.js"></script>
<script src = "js/Board.js"></script>
<script src = "js/Space.js"></script>
<script src = "js/Player.js"></script>
<script src = "js/Token.js"></script>
After
<script src = "js/Game.js"></script>
<script src = "js/Board.js"></script>
<script src = "js/Space.js"></script>
<script src = "js/Token.js"></script>
<script src = "js/Player.js"></script>
or
the module approach in which it looks like this I believe the scripts can be in any order if you do it this way
<script type="module"src = "js/Game.js"></script>
<script type="module"src = "js/Board.js"></script>
<script type="module"src = "js/Space.js"></script>
<script type="module" src = "js/Player.js"></script>
<script type="module" src = "js/Token.js"></script>
<script src = "js/app.js"></script>
then from my experience you can use
export default Token;
to then import into Player.js
import Token from "./Token.js";
Hopefully this helps. For how powerful OOP is be this whole course challenge is poorly done in my opinion. :(
Jeffrey Meesters
Full Stack JavaScript Techdegree Graduate 16,657 PointsJeffrey Meesters
Full Stack JavaScript Techdegree Graduate 16,657 PointsHi,
Not sure what you're working on but I guess it is the four in a row tutorial? All JS should be imported in the correct order in your index.html. So please check if you imported it with a script tag in your index.html and if so that the order also is correct.
Greetings,
Jeffrey