Sunday 12 March 2017

Using es6 in a package json bin script

I have built a script with node using the bin field. Here is my package.json :

  ...
  "babel": {
    "presets": [
      "latest",
      "stage-1",
      "react"
    ],
    "plugins": [
      "transform-runtime"
    ]
  },
  "bin": {
      "reducer": "./generators/reducer.js && standard --fix src/redux/*.js"
  },
  ...

It is a react app based on the following boilerplate : https://github.com/kriasoft/react-static-boilerplate

I have access to es6 features inside my app but not inside the reducer scripts executed with the reducer command(see bin property in package.json above).

How can I have access to es6 features inside my node executable files launched by the keywords inside the bin property ?

reducer.js script file :

#!/usr/bin/env node

var program = require('commander')
var generateTemplate = require('./generateTemplate')

program
    .arguments('<name>')
    .action((name) => {
         console.log(name)
      }
    })
    .parse(process.argv)

generateTemplate.js :

#!/usr/bin/env node

var ejs = require('ejs')
var fs = require('fs')

export default function generateTemplate (templateArgs, newFilePath, template) {
  var output = ejs.render(template, templateArgs)
  fs.writeFile(newFilePath, output, (err) => {
    if (err) {
      return console.log(err)
    }
    console.log(`=============================================================\n\n ${newFilePath} generated !\n`)
  })
}

It gives me the following error :

/Users/alexandre/Documents/github/csgames-training/training/client/generators/generateTemplate.js:6 export default function generateTemplate (templateArgs, newFilePath, template) { ^^^^^^ SyntaxError: Unexpected token export at Object.exports.runInThisContext (vm.js:78:16) at Module._compile (module.js:543:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at Object. (/Users/alexandre/Documents/github/csgames-training/training/client/generators/reducer.js:13:24) at Module._compile (module.js:571:32)



via AlexB

No comments:

Post a Comment