mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
8.96k stars 22.45k forks source link

Express - Unexpected token error after following instructions? #34191

Open ajaffary opened 2 weeks ago

ajaffary commented 2 weeks ago

MDN URL

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Displaying_data/Home_page

What specific section or headline is this issue about?

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Displaying_data/Home_page#what_does_it_look_like

What information was incorrect, unhelpful, or incomplete?

I am receiving the following errors after copying/pasting the updated async handler code from this module:

SyntaxError: Unexpected token (137:1)
    at pp$4.raise (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:2488:13)
    at pp.unexpected (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:623:8)
    at pp$3.parseExprAtom (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:2011:10)
    at pp$3.parseExprSubscripts (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1872:19)
    at pp$3.parseMaybeUnary (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1849:17)
    at pp$3.parseMaybeUnary (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1840:26)
    at pp$3.parseExprOps (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1791:19)
    at pp$3.parseMaybeConditional (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1774:19)
    at pp$3.parseMaybeAssign (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1750:19)
    at pp$3.parseExpression (/Users/abbas/Documents/code/express/express-locallibrary-tutorial/node_modules/acorn/dist/acorn.js:1722:19)

I have searched around for similar errors and cannot find anything, nor can I find anything wrong with the code. I am not sure where else to look for an error. I'm connected to my MongoDB database, and all the code has been working prior to this, i.e. with res.send("NOT IMPLEMENTED: Site Home Page"); inside the handler function.

What did you expect to see?

I expected to see the browser show the same information as shown in the example.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details * Folder: `en-us/learn/server-side/express_nodejs/displaying_data/home_page` * MDN URL: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Displaying_data/Home_page * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/learn/server-side/express_nodejs/displaying_data/home_page/index.md * Last commit: https://github.com/mdn/content/commit/0a9dc26d36f530517745e630efbddbf2c97ca451 * Document last modified: 2024-04-26T12:29:34.000Z
Josh-Cena commented 2 weeks ago

If you are using npm, what's your output of npm ls acorn?

ajaffary commented 2 weeks ago
express-locallibrary-tutorial@0.0.0 /Users/abbas/Documents/code/express/express-locallibrary-tutorial
└─┬ pug@2.0.0-beta11
  ├─┬ pug-code-gen@1.1.1
  │ └─┬ with@5.1.1
  │   ├─┬ acorn-globals@3.1.0
  │   │ └── acorn@4.0.13 deduped
  │   └── acorn@3.3.0
  └─┬ pug-lexer@3.1.0
    └─┬ is-expression@3.0.0
      └── acorn@4.0.13
Josh-Cena commented 2 weeks ago

Thanks, @hamishwillee it looks the package versions specified in the tutorial are outdated—maybe it's time for a rewrite? :)

hamishwillee commented 2 weeks ago

@Josh-Cena Yee of little faith. The dependencies were updated a week or so ago to match the very latest :-). I'd been trying not to do this because this uses the express application generator and I wanted to match that tools default output as much as possible to ease the job for developers.

@ajaffary Was probably unlucky enough to do the skeleton creation before then, which is why they look old. However that shouldn't be the cause of this problem since nothing else changed other than the dependencies, and they worked both before and after with no other changes.

@ajaffary The text that changed last week was in https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website#package.json - near the end:

image

Did you create the skeleton before last week? If not, that tells me I probably need to make this more visible.

In any case, can you rebuild with those dependencies and see what happens.

As I say though, I don't think that is the cause (though it may be). The worked project that was created by going through this tutorial is here: https://github.com/mdn/express-locallibrary-tutorial

It won't precisely match yours, but you should be able to compare and see if there are any obvious differences - and make sure your dependencies match?

Josh-Cena commented 2 weeks ago

There's another one a bit further up, right below the "package.json" header", that's outdated.

I wouldn't really recommend copy/pasting package.json. Running npm install everything you need sounds easier.

hamishwillee commented 2 weeks ago

There's another one a bit further up, right below the "package.json" header", that's outdated.

Nope, that's the output generated by the application generator, as stated in the docs. What I am thinking of doing though is moving the instructions to patch this higher up so the text matches.

So the idea here is that they generate most of the code framework using the application generator. In the past the app gen was pretty close to current, so the slight difference in versions didn't matter. Now it does.

I wouldn't really recommend copy/pasting package.json. Running npm install everything you need sounds easier.

Probably not. The package file is generated by the application generator, and not all values are done with ^. So if you do update you only get a semantic update in the current minor.patch version.

In any case I actually want to fix the versions used so that I can choose when to update them. Why? Because that means this doesn't get broken whenever there is a big update - or at least the code runs for the listed dependencies.

Of course you're right as a general thing, but I've been maintaining this since I wrote it, and every time adding more flexibility to dependencies makes the cost of maintenance higher.