1

What do you guys think about making constants in Javascript. I want to do it in the best possible way. I know that constants do not really exist but I wrote this and I was not able to change the values after the export.

Is there a need for constants?

Is there a work around?

How can we use them global (without require('const')); ?

// Const
var constants = {
    'PATH1' : __dirname + '/path..../',
    'PATH2' : __dirname + '/path/..../'
};
module.exports = function(key) {
    return constants[key];
};
//console.log(constants('PATH1'));

Would be glad if I get some feedback, like your thoughts about these questions.

I wish you a good day.

12
  • 2
    Sure, it is wanted we call it "knowledge sharing" Commented Jul 26, 2013 at 7:17
  • I think you want programmers.stackexchange.com Commented Jul 26, 2013 at 7:18
  • 1
    possible duplicate of Are there constants in Javascript?. You are probably particularly interested in this answer: stackoverflow.com/a/4637056/218196. For Node.js, you have to use the global variable instead (I assume): nodejs.org/api/globals.html. Commented Jul 26, 2013 at 7:23
  • 2
    Oh, apparently Node.js does support the const declaration. See How do you share constants in NodeJS modules? (possibly a duplicate as well). Commented Jul 26, 2013 at 7:27
  • 1
    By now: the const keyword has been mentioned a couple of times, but for browser implementations, you could simply use Object.defineProperty(constants, 'PATH1', {value: 'theValue'}); because the property defaults to non-configurable, non enumerable and non-writeable, it effectively turns into a read-only, constant value property Commented Jul 26, 2013 at 7:32

2 Answers 2

3

It's ugly/difficult to use globals in node for a good reason: globals are bad.

Doing what you want is as easy as:

// config.json
module.exports = {
  somePath:    "/foo",
  anotherPath: "/foo/bar"
};

Use in a file

// a.js
var config = require("./config");
config.somePath; //=> "/foo"

Use in another file

// b.js
var config = require("./config");
config.anotherPath; //=> "/foo/bar"

Just recently, in another question, I went into depth about how it's completely unnecessary to use globals in node.js

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this is how I doing it at the moment. My simple problem is just to require the config.json again and again..
@Silom, that's not a "problem" anymore than saying I have to use () around a JavaScript if condition; that's just a part of working with node.js. Adding one line of code to call your global app config into a module is not a big deal at all.
That is right, but this is my question. See if I write into a package.json main : "foo.js" and run node . he will run foo.js instead of index.js. So why is there nothing like a config: "config.json" and I can use it everywhere without requiring it every time. At least you are right it is not a big deal, it is just interesting to find a new easy way.
0

This is a bad idea, but it can be done. I've only tested this in Node.js. It may or may not work in the browser. If you substitute global for window it might work reliably in the browser.

Here you go:

app.js:

Object.defineProperty(global, 'myConst', {
  get: function() {
    return 5;
  }
})

myConst = 6

console.log(myConst) //5

Run like:

node app.js

Tested with v0.10.3.

5 Comments

Thanks for your answer. But what could I do else ? I have like on path to a file and I want to use it everywhere. Should I do a fs.resolve in every file?
I don't understand. You can use this everywhere. Just require your constants in from one file in one location in your application and it'll work. For example, let's say you have a main.js. Just do require('./mycontants') at the top of main.js.
that's the point you have to require it. If I got 10 files where I have to use it I always have to require(path/to/const.js) just to use another return value for another path.
No you don't. You only require it once. If the rest of your 10 files are required somewhere in your app, they can all access the constants if you only require it once in one of the files because of global. That's what I'm trying to tell you.
@naomik There is a difference between a bad idea and a bad answer. Sometimes people just want to learn techniques. They have their own reasons, perhaps academic? Your analogy is awful. We're not talking about actions with irrevocable consequences. We're talking about learning.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.