Stack Exchange
Passing arguments to class constructors
I've written the below module, but importing it doesn't work as expected. Files (abbreviated for simplicity):
templates.js
'use strict';

export default class Templates {
    constructor (args) {
        this.files = [{
            name: `${args.name}.js`,
            path: 'src/',
            content: ''
        }];
        /* code omitted */
    }
};

I'm trying to use it like so:
index.js
import Templates from './templates'

const opts = {name: 'app'};
/* code ommited */
console.log('Templates >>', typeof Templates);
console.log('Templates >>', new Templates());

let tmpls = new Templates(opts);
console.log(tmpls.files[0].name);

But I get the following error trace in console.
npm ERR! Linux 3.16.0-38-generic
npm ERR! argv "/home/wesleycoder/.nvm/versions/node/v5.1.0/bin/node" "/home/wesleycoder/.nvm/versions/node/v5.1.0/bin/npm" "init"
npm ERR! node v5.1.0
npm ERR! npm  v3.3.12

npm ERR! Cannot read property 'name' of undefined

And yes this is a ~/.npm-init script for who may ask.
Edit:
Thanks to @low_ghost for the useful response.
I've figured out that my problem wasn't about exporting and importing classes.
It was actually about passing arguments to this class but not about not passing the argument. So I've changed the title for concision.
The console.log(new Templates()) without any argument was intentional in my code.
The error that I was trying to solve was about instantiating the object and getting the files array, but I was passing the arguments directly from the commander.js library and the arguments passed were "overkill" to this task, so I simplified the arguments by taking the input from commander.js and assigning each one in a property in an empty object then passing this object along the constructor, this allowed me to take readline-sync and read input from options not passed and make the script more awesome, after this all worked fine.
Would be fine if commander.js had any option to give the options passed without the extra weight of the library properties.
Edit2:
After all commander.js was not useful because I couldn't pass the arguments along npm init command to the script it self.

Just to clarify: this issue has nothing to do with Babel nor ES6 specifically.

Name is undefined due to the absence of a parameter in the constructor call. Best solution, depending on intent, would be to check for the existence and type of name:
 'use strict';

 export default class Templates {
   constructor (args) {
     this.files = (args.name && typeof arg.name === "string")
       ? [{
           name: `${args.name}.js`,
           path: 'src/',
           content: ''
         }]
       : [];
    /* other code */
   }
};

if you are ok with the ternary. This gives back an empty array as this.files if arg.name is not present or is not a string.
EDIT:
Or, as madox2 suggests, provide a default. Though I would do it with a default for name rather than for the entire args object. like:
 constructor({ name = 'defaultName', ...otherArgs } = {})

that way if you called:
 new Templates({ dir: 'repo' })

you would still have the name default and, if you take the ellipsis literally, you could get dir from otherArgs.dir. The = {} part allows calling
 new Templates()

and still getting the default name.

{ name = 'defaultName', ...otherArgs } is not valid javascript

@vkurchatkin: sure it is. Look up destructuring.

I mean ...otherArgs part

@vkurchatkin: I see. That's an ES7 proposal though which can be enabled in Babel.

ES7 implies that it will be a part of 2016 spec edition, but it's not known at this point. Anyway, it's better to avoid proposed syntax extensions unless question is directly related.

yeah, could have been more clear there. Probably pretty safe to use, it's scattered pretty ubiquitously through modern React code. However, I believe it's only stage 2.

4 years later, adoption of REST parameters has caught up... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

Constructor of Templates class takes object as parameter. If you don't pass it, args parameter is undefined and it will fail when you call args.name. Try this:
console.log('Templates >>', new Templates({}));

or: 
console.log('Templates >>', new Templates({name: 'myName'}));

You can also set default parameter value in your constructor and then call it without parameter:
constructor (args = {name: 'defaultName'}) {
    //...

