How works "require" after "export default" in EC6?

198 views Asked by At

Following a tutorial I found this chunk of code in a file:

export default require ('knex') ({
   client : 'mysql';
   connection : {
      host : 'localhost',
      user : 'root',
      password: '',
      database : 'graph',
      charset : 'utf8',
   }
});

This file is being imported in another as :

import Knex from './knex'

When I run the app I get this error

Unexpected token (1:20)
export default knex require ('knex') ({
    client : 'mysql';
    connection : {
        host : 'localhost',
    }
})

I want to fix this but I don't understand how require works when is preceded for export default.

Thanks!

1

There are 1 answers

2
WebD On

Your export default require is not valid. Try this instead:

export default {
      client : 'mysql';
      connection : {
        host : 'localhost',
        user : 'root',
        password: '',
        database : 'graph',
        charset : 'utf8',
   }

It exports an objet that you can later import with import myObj from './myfile'.