SyntaxError: Unexpected token = inside /node_modules/hapi/lib/server.js:107
SyntaxError: Unexpected token = inside /node_modules/hapi/lib/server.js:107
我开始使用 hapijs 并设置了一个新的实践项目以进行更深入的研究。我成功地设置了一次并且它工作正常但是突然它开始抛出错误而 运行 节点 server.js 如下所示。
/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)
您可以通过以下其中一种解决方案解决此问题。
解决方案 1。由于 Hapi 版本 17.x 仅支持 node v8.9.0 及以上版本。因此,如果您正在使用 Hapi 17.x 并且不想降级 hapijs 版本,那么您必须使用 v8.9.0 以上的节点版本。您可以在 NVM(节点版本管理器)的帮助下更改节点版本。
第 1 步:要安装 nvm,您需要 运行 按照命令
npm install nvm
第2步:现在安装nvm后,您可以在系统上管理多个版本的node js,并且可以通过一条命令切换到特定版本。您可以使用以下命令安装特定版本的 node js。
nvm install v8.9.1
第 3 步:最后,您现在可以使用命令 nvm ls
列出节点版本,并继续您需要的特定节点版本 运行 nvm use v8.9.1
切换v8.9.0以上的节点版本后不会出现此错误
解决方案 2。如果你不想升级你的节点版本,那么你必须需要降级你的 hapijs 版本。您可以使用以下命令执行此操作。
npm install hapi@16.0.0
对于 2020 年面临此问题的任何人:
You need to use node 12 if you're going to use hapi 19. Note that hapi 19 has very recently been released, not all tutorials will fit perfectly. If you want to learn hapi I'd suggest you stick with hapi 18 until all educational materials are ready as it will be easier for you to follow along.
我开始使用 hapijs 并设置了一个新的实践项目以进行更深入的研究。我成功地设置了一次并且它工作正常但是突然它开始抛出错误而 运行 节点 server.js 如下所示。
/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)
您可以通过以下其中一种解决方案解决此问题。
解决方案 1。由于 Hapi 版本 17.x 仅支持 node v8.9.0 及以上版本。因此,如果您正在使用 Hapi 17.x 并且不想降级 hapijs 版本,那么您必须使用 v8.9.0 以上的节点版本。您可以在 NVM(节点版本管理器)的帮助下更改节点版本。
第 1 步:要安装 nvm,您需要 运行 按照命令
npm install nvm
第2步:现在安装nvm后,您可以在系统上管理多个版本的node js,并且可以通过一条命令切换到特定版本。您可以使用以下命令安装特定版本的 node js。
nvm install v8.9.1
第 3 步:最后,您现在可以使用命令 nvm ls
列出节点版本,并继续您需要的特定节点版本 运行 nvm use v8.9.1
切换v8.9.0以上的节点版本后不会出现此错误
解决方案 2。如果你不想升级你的节点版本,那么你必须需要降级你的 hapijs 版本。您可以使用以下命令执行此操作。
npm install hapi@16.0.0
对于 2020 年面临此问题的任何人:
You need to use node 12 if you're going to use hapi 19. Note that hapi 19 has very recently been released, not all tutorials will fit perfectly. If you want to learn hapi I'd suggest you stick with hapi 18 until all educational materials are ready as it will be easier for you to follow along.