0

I am building an app using Node and ES6. This app has a library that I want to use across projects. For the sake of this question, the directory structure of the library looks like this:

lib
  logging
    logManager.js
    logEntry.js
  security
    user.js
    role.js

My actual structure is different. I chose logging and security to make the question more approachable. Still, my code currently looks like this:

logManager.js

'use strict';

var User = require('../security/user.js');
class LogManager {
  constructor(props) {
    this.user = props.user;
  }
}
module.exports LogManager;

logEntry.js

'use strict';
class LogEntry {
  constructor(props) {
  }
}
module.exports LogEntry;

user.js

'use strict';
class User {
  constructor(props) {
  }
}
module.exports User;

role.js

'use strict';
class Role {
  constructor(props) {
  }
}
module.exports Role;

My mind set may be in the object-oriented Java / C# worlds. However, I'm trying to figure out how to package these four files into a single file I can distribute. In an attempt to do this, I have added a gulp task that looks like this:

gulp.task('package', function() {
    return gulp.src(input.js)
        .pipe(jshint())
        .pipe(jshint.reporter('default'))       
        .pipe(babel())
        .pipe(concat(output.js))
        .pipe(gulp.dest('dist'));
    ;    
});

This task puts all four files into a file called dist/library.js. However, I have several issues. First, JSHint complains about the use of strict in each of my files. So, I removed the 'use strict' lines just for the sake of trying to move forward and getting by JSHint (which I don't want to do). But I feel I'm really off on something because when the concatenation happens, the require statement in logManager.js breaks.

It feels like everything works as individual components. However, when I attempt to bundle them altogether into library.js, it breaks. What am I missing?

2

1 Answer 1

-1

You have a syntax error in your module exports.

// this is wrong syntax (missing "=")
module.exports LogManager;

As you already use babel, you could switch over ES6-modules

// default export of module (ES6 syntax)
export default LogManager;

// importing modules (ES6 syntax)
import User from 'security/user';
2
  • So, are you recommending I `import User from 'security/user' in myLibrary.js? Nov 25, 2015 at 15:41
  • as Yury Tarabanko commented your export syntax is wrong. You can fix it and use commonJS modules or by using ES6 module syntax
    – Fdr
    Nov 25, 2015 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.