1
const routes = (app) => {
  app.route('/contact')
  .get((req, res, next) => {
      // middleware
      console.log(`Request from: ${req.originalUrl}`)
      console.log(`Request type: ${req.method}`)
      next();
    }, (req, res, next) => {
      res.send('GET request successful!!!!');
  })

  .post((req, res) =>
    res.send('POST request successful!!!!'));

  app.route('/contact/:contactId')
  .put((req, res) =>
    res.send('PUT request successful!!!!'))

  .delete((req, res) =>
    res.send('DELETE request successful!!!!'));
}

export default routes;

Produces this error when executing:

export default routes;
^^^^^^

SyntaxError: Unexpected token export

I'm actually trying to follow along in a training video so I'm a bit new to this. From my understanding he's trying to use ES6 and I know some commands, like import, aren't available in node ver 9 natively. Could this be one of them? Any alternatives?

bennyharassi
  • 81
  • 1
  • 3
  • 9

6 Answers6

4

Most likely your Node project is not setup to use ES6 module loading.

Node uses an older standard of module loading called the CommonJS standard. In order for your project to use the ES6 module loading the way you have it you need to use babel and a tool like webpack.

If you search my name and tutorial I show how to set this up in less than 3 minutes. In the example, it's also setting up a react project, you would just be interested in everything else besides that.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • They had me setup Babel in the package.json file here: { "name": "crm", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon ./index.js = --exec babel-node -e js" } – bennyharassi Mar 12 '18 at 20:29
  • babel's ability to load modules is due to a plugin called babel-loader which is separate from other babel plugins like babel-cli etc... – Chris Hawkes Mar 12 '18 at 20:32
  • I see, would that be babel-webpack-plugin now? https://nourish.je/hello-webpack-2-goodbye-babel-loader/ – bennyharassi Mar 12 '18 at 20:35
3

Try to use module.exports.routes;

You want to use ES6 Module syntax. This means that there should be support for ES6 Module syntax.

You can proceed with at least two way:

  • Use babel npm package to transpile ES6 style
  • Refactor to use syntax above.
KarlR
  • 1,545
  • 12
  • 28
  • routes(app) ^ TypeError: routes is not a function – bennyharassi Mar 12 '18 at 20:14
  • In another file You can write (index is the file where routes are exported): var express = require('express'); var routes = require('./index'); var app = express(); and then routes(app). – KarlR Mar 12 '18 at 20:32
2

You need to create a .babelrc file in your root folder as same as server.js or index.js and add and object like this:

{
    "presets": [
        "env",
        "stage-0"
    ]
}

this will tell node to use the preset env and stage-o. I assume that you download the babel plugin.

Just in case remember config the server.js (in my case) with this:

"scripts": {
    "start": "nodemon ./server.js --exec babel-node -e js"
  },

to use ES6 Module syntax.

1

Node version 8.x.x does not support import. Use the latest version of node 9.x.x and the error will go away. Also, you can us babel to transpile your code.

Anup
  • 627
  • 5
  • 19
1

You need to create in your file as same as index.js and add

module.exports = router;

Add This in index.js

const postRoutes=require('./routes/posts')
app.use('/post',postRoutes);
0

You have to add "type": "module" just before "scripts" in your package.json and change every:

const express=require('express');

To:

import express from 'express'
Tyler2P
  • 2,324
  • 26
  • 22
  • 31