6

I've been making a script via tutorial, but when I'm trying to run index.js via nodemon run index.js, I get syntax error in pointing to import express from 'express'; .

I have nodemon installed, for bd I've also have mongo and mongod running. Tutorial might be outdated or have something installed/running without being told.

My index.js

import express from 'express';
import dbConfig from './config/db';
const app = express();
dbConfig();
const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
  if (err) {
    console.error(err);
  }{
    console.log('App listen to port: ${PORT}');
  }
});

package.json:

{
  "name": "meetup-backend",
  "version": "0.0.1",
  "main": "index.js",
  "scripts": {
    "dev": "NODE_ENV=development nodemon dist/index.js",
    "build:watch": "babel -w --out-dir=dist ./src",
    "clean": "rimraf dist",
    "lint": "eslint src",
    "lint:fix": "eslint --fix src"
  },
  "repository": {},
  "author": "Emanuel Quimper",
  "license": "MIT",
  "dependencies": {
    "axios": "^0.16.1",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "babel-register": "^6.26.0",
    "body-parser": "^1.18.3",
    "detect-port": "^1.3.0",
    "es6": "0.0.7",
    "express": "^4.16.4",
    "jsonwebtoken": "^7.3.0",
    "mongoose": "4.7.7",
    "morgan": "^1.9.1",
    "passport": "^0.3.2",
    "passport-jwt": "^2.2.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-object-rest-spread": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "eslint": "^3.19.0",
    "eslint-config-equimper": "^1.6.2",
    "nodemon": "^1.18.10",
    "rimraf": "^2.6.1"
  }
}

As i run nodemon index.js I get:

 nodemon index.js
[nodemon] 1.18.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js`
/Users/ari-mikkopenttila/Documents/GitHub/youtubeMeetupAppReactNativeNode/meetup-backend/src/index.js:3
import express from 'express';
       ^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
[nodemon] app crashed - waiting for file changes before starting...

I just cant seem to find what causes error and how to fix it. Help/advice needed and apologies if problem is easy to fix.

Ari
  • 85
  • 1
  • 1
  • 8
  • Look at https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node/45854500 – Leo Feb 12 '19 at 13:06
  • By the way, @YvetteColomb, I had edited my answer to better fit the question before you deleted it. (Though I understand why you did) – Seblor Feb 12 '19 at 13:06
  • You have to transpile your code with babel before. Run `npm run build:watch` then `npm run dev` in seperate terminals – Marvin Fischer Feb 12 '19 at 13:09
  • Thank you @MarvinFischer , I got it working! – Ari Feb 12 '19 at 14:51
  • 1
    I fixed the same error (in my case nodemon) my updating node to the latest version (mine was really old) – Gerry Dec 24 '19 at 17:21
  • Thank you so much @Gerry ! I had spent all of 2020's confinement developing on my personal, up to date, laptop and didn't notice that my work's PC had Node 6 on it... you just saved my day. I was also using nodemon and having this exact error message --> updating Node fixed it – DixiPoowa Jul 16 '20 at 07:43

0 Answers0