1

I am using Linux mint. My node version is 9.4 and nmp version is 5.6

See the error i got when run "node server"

/var/www/html/mean/firstapp/server.js:1
(function (exports, require, module, __filename, __dirname) { import mongoose from 'mongoose';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at new Script (vm.js:51:7)
    at createScript (vm.js:138:10)
    at Object.runInThisContext (vm.js:199:10)
    at Module._compile (module.js:624:28)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)

Generally i am getting this error from MongoDB

MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] 
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] 
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] 
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-01-31T11:15:50.975+0530 I CONTROL  [initandlisten] 
2018-01-31T11:25:28.254+0530 E -        [main] Error loading history file: FileOpenFailed: Unable to fopen() file /home/ranjit/.dbshell: No such file or directory
Ranjit
  • 1,684
  • 9
  • 29
  • 61

2 Answers2

2

Imports, also known as ES modules are not natively supported by node. Node uses CommonJS by default (require).

Node 9 does now support ES modules but it's behind a flag.[1]

To use your server.js with ES modules you need to rename it to server.mjs and launch it like this:

node --experimental-modules server.mjs

[1] https://nodejs.org/api/esm.html

Pandelis
  • 1,854
  • 13
  • 20
1

import is not supported syntax for CommonJS module system in node.

You either use require, or transpile your Node.js code with something like babel-node.

https://babeljs.io/docs/usage/cli/#babel-node

CharlieBrown
  • 4,143
  • 23
  • 24