nodejs项目报错|node-localstorage错误
系统错误提示:
E:\***\project\gais-middleware\node_modules\signal-exit\dist\cjs\index.js:162
const ev = opts?.alwaysLast ? 'afterExit' : 'exit';
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (E:\liuy\project\gais-middleware\node_modules\write-file-atomic\lib\index.js:9:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
经过排查,导致错误的代码为:
const LocalStorage = require('node-localstorage').LocalStorage;
const localStorage = new LocalStorage('./data');
错误分析:
1.localStorage插件提示的报错信息,错误信息表明Node.js 环境不支持可选链操作符(Optional Chaining Operator)?.。可选链操作符是在 ECMAScript 2020(ES11)中引入的,如果 Node.js 版本低于这个版本,那么它将不支持这个特性。
2.完全支持 ECMAScript 2020 特性的 Node.js 版本是 14.0.0 及更高版本
3.查看我自己的nodeJs版本。
PS E:\***\project\gais-middleware> nvm ls
18.15.0
16.4.2
* 12.22.12 (Currently using 64-bit executable)
8.17.0
版本低于14.0.0,切换版本(没有版本切换的需要下载14.0.0及更高版本)。
nvm use 16.4.2
解决:
PS E:\***\project\gais-middleware> node app.js
启动成功,访问 http://localhost:8071