Issue
I have been doing javascript for a while and decided to start node.js.
I am running node.js and express on a raspberry pi (but I doubt this is the root of the problem).
I am making a chat app with socket.io, and the instructions said to create a package.json:
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {}
}
Then I ran the command npm install [email protected]
.
After that, I made an index.js
file.
This is the code.
const app = require('express')();
const http = require('http').createServer(app);
app.get('/', (req, res) => {
res.send('<h1>Hello world</h1>');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
Error code:
/home/pi/Desktop/index.js:4
app.get('/', (req, res) => {
SyntaxEror: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Thanks!
Solution
Arrow functions are an ES2015 feature. Generally, environments after 2015 support arrow functions (and other modern syntax). Node 0.10.29 was released in 2014.
To fix it, install the latest long-term version that your machine supports: probably 14.6.0. See here.
Uninstall Node with
sudo apt remove nodejs
and then install it (which will fetch the latest version)
sudo apt install nodejs