Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this simple export is giving me error in nodejs?

I got this example product js file and i am exporting it and using it in the node routes

let products = [
    {
      _id: "1",
      name: "Airpods Wireless Bluetooth Headphones",
    },
    {
      _id: "2",
      name: "iPhone 11 Pro 256GB Memory",
    },
    {
      _id: "3",
      name: "Cannon EOS 80D DSLR Camera",
    },
  ];
  
  export default products

and i am getting that data in the server.js like this

const products = require('./data/products')

const app = express()

app.get('/', (req, res) => {
    res.json('api running')
})

app.get('/api/products', (req, res) => {
    res.json(products)
})


app.get("/api/products/:id", (req, res) => {
    const product = products.filter(p => p._id === req.params.id);
     res.json(product);
});

but whenever i try to run the server i got this error Unexpected token 'export'.
Why this simple export is giving me error?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use common js module syntax instead of es6 module syntax for exporting products:

module.exports = products;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading