node.jsexpresspassengerdreamhost

Basic Express/Node Function Syntax Not Working


On my Mac, I can create a simple "Hello World" Node/Express app like this:

const express = require('express')
const app = express()

app.get('/', function(req, res){
  res.send('Yo')
})

app.listen(3000, () => 
  console.log('Listening on port 3000...')
)

It works great. But when I install my Node app on my web host (DreamHost), I make sure I'm on the same version of Node and NPM, and I do npm install and then restart the server.

Passenger complains about the syntax:

/home/labrumco/progressbox.net/app.js:9
app.listen(3000, () => 
                  ^
SyntaxError: Unexpected token )

Why would it reject that syntax? My guess is some kind of syntax dependency, but I'm too new to Node to know the difference. :)

If I change it to this it works:

app.listen(3000, function() {
  console.log('Listening on port 3000...')
})

function(){ works instead of () =>

Why is this happening?

--- Update ---

Here is my package.json file:

{
  "name": "...",
  "version": "1.0.0",
  "description": "...",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Clifton Labrum",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "node-fetch": "^2.2.0",
    "nodemon": "^1.18.3",
    "request": "^2.88.0"
  },
  "repository": {
    "type": "git",
    "url": "..."
  }
}

Solution

  • I resolved this by informing Passenger of the node version through the .htaccess file.

    Make sure you follow the guide to use nvm to install your version of node.

    nvm install v10.18.1
    nvm alias default v10.18.1
    

    In your project directory, create a file named .htaccess and add the following line:

    PassengerNodejs /home/username/.nvm/versions/node/v10.18.1/bin/node
    

    Make sure you replace username with your username and the node version you need

    Then restart the server (touch tmp/restart.txt) and Passenger should use the specified version of node.