Search code examples
node.jsversionhapi.js

SyntaxError: Unexpected token = inside /node_modules/hapi/lib/server.js:107


I started working with hapijs and setup a new practice project with it for the deeper-dive. I successfully set it once and it works fine but suddenly it started throwing error while running node server.js as below.

/Library/WebServer/Documents/hello_hapi/node_modules/hapi/lib/server.js:107
    decorate(type, property, method, options = {}) {
                                             ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/Library/WebServer/Documents/hello_hapi/node_modules/hapi/lib/index.js:5:16)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

Solution

  • You can solve this problem with following one of the solutions.

    Solution 1. As Hapi Version 17.x only supports node v8.9.0 and over. So if you are working with Hapi 17.x and you do not wants to downgrade hapijs version then you must need to use node version above v8.9.0. You can change node version with the help of NVM (node version manager).

    Step 1: To install nvm you need run following command

    npm install nvm
    

    Step 2: After installing nvm now you can manage multiple versions of node js on your system and you can switch to specific version with a single command. You can install specific version of node js with following command.

    nvm install v8.9.1
    

    Step 3: At last you can now list node version with command nvm ls and to move on the specific version of node you need to run nvm use v8.9.1

    After switching node version above v8.9.0 this error will not occur.

    Solution 2. If you don't want to upgrade your node version then you must need to downgrade your hapijs version. You can do that with the following command.

    npm install [email protected]