Search code examples
javascriptecmascript-6commonjs

Convert CJS to ES6


How can I convert these lines of code to ES6?

const basename = path.basename(__filename);
const config = require(__dirname + '/../config/config.json')[env];

Solution

  • You don't need to convert anything to ES6, it's perfectly valid ES6 - it just doesn't use any new features that were introduced in ES6 from my knowledge. You could use destructuring if you really wanted to:

    const { env: config } = require(__dirname + "/../config/config.json");
    

    But ES6 is not a fancy new language, it's just got some new features you can use if you need them.