Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require and Coffeescript #3

Closed
karellm opened this issue Oct 25, 2013 · 4 comments
Closed

require and Coffeescript #3

karellm opened this issue Oct 25, 2013 · 4 comments

Comments

@karellm
Copy link

karellm commented Oct 25, 2013

require('dotenv')() compiles to require('dotenv') in coffeescript, causing the following error:

dotenv.load();
       ^
TypeError: Object function dotenv() {
  dotenv = {
    version: package_json.version,
    environment: process.env.NODE_ENV || "development",

    _loadEnv: function() {
      return dotenv._setKeysAndValuesFromEnvFilePath(".env");
    },
    _loadEnvDotEnvironment: function() {
      return dotenv._setKeysAndValuesFromEnvFilePath(".env."+dotenv.environment);
    },
    _setKeysAndValuesFromEnvFilePath: function(filepath) {
      try {
        var data        = fs.readFileSync(filepath);
        var content     = data.toString().trim();
        var lines       = content.split('\n');

        for (var i=0; i<lines.length; i++) {
          var key_value_array = lines[i].split("=");
          var key             = key_value_array[0].trim();
          var value           = key_value_array[1].trim();

          if (value.charAt(0) === '"' && value.charAt(value.length-1) == '"') {
            value               = value.replace(/\\n/gm, "\n");
          }

          value               = value.replace(/['"]/gm, '');

          process.env[key]    = value;
        }
      } catch (e) {
      }

      return true;
    },
    load: function() {
      dotenv._loadEnvDotEnvironment();
      dotenv._loadEnv();

      return true;
    },
  };

  return dotenv;
} has no method 'load'

A quick workaround is to wrap it using backtick to use raw javascript. Couldn't you just remove the need for the trailing()` ?

@maxbeatty
Copy link
Contributor

I'm using dotenv with CoffeeScript and getting it to compile correctly.

dotenv = require('dotenv')()
dotenv.load()

Compiled:

var dotenv;

dotenv = require('dotenv')();

dotenv.load();

@motdotla
Copy link
Owner

@karellm can you paste your coffeescript version in here:

coffee --version

I'm at 1.6.3

@motdotla
Copy link
Owner

Hi @karellm, I'm closing this having tested on my own side. If you continue to run into the error, just post your coffee script version and I'll re-open. Hope it got working for you!

@karellm
Copy link
Author

karellm commented Oct 31, 2013

Hi @scottmotte,

Sorry for the late answer and trouble. I may have made a mistake somewhere because it is compiling just fine with coffeescript 1.6.3.

Thanks for the great support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants