node mongo.jsでエラー

node mongo.jsを実行するもエラーとなり実行できず解決方法が判明したのでシェア。

 

★コマンド

[vagrant@localhost nodejs]$ node mongo.js

 

★エラー内容

/home/vagrant/nodejs/node_modules/mongodb/index.js:4
const core = require('mongodb-core');
^^^^^
SyntaxError: Use of const in strict mode.
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/vagrant/nodejs/mongo.js:1:81)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

 

★解決方法

nodeのversionがmongodbのversionとかみ合っていなかった模様。

nvmでnodeのversionをUPしたところ上記のエラーは出なくなった。

 

$ git clone https://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh
$ nvm help
$ nvm list-remote /* show Node.js version list */
$ nvm install v5.11.0

 

★結果

[vagrant@localhost nodejs]$ node mongo.js
current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connected to db

 

解決...