简体   繁体   中英

export default routes; SyntaxError in NodeJS

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?

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.

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.

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.

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

I encountered the same problem laterally i realized i have used the require in place of import ES6 syntax for importing the dependencies. I just solved it by using module.exports=router

NOTE: Incase you use ES6 Importing, then the code you wrote in the question will work perfect. Here you can find an example of my Code.

const express = require('express');
let router = express.Router();

router.post('/', (req, res) => {
 res.status(201).json({
     success: true
  });
});
// make the export
module.exports = router;

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);

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM