node.js global variables?

2018-12-31 16:20发布

I asked here: node.js require inheritance?

and was told that I can set variables to the global scope by leaving out the var.

This does not work for me.

ie:

_ = require('underscore');

Does not make the _ available on required files. I can set with express's app.set and have it available elsewhere though.

Can somebody confirm that this is supposed to work? Thanks.

7条回答
忆尘夕之涩
2楼-- · 2018-12-31 16:53

What about a global namespace like global.MYAPI = {}

global.MYAPI._ = require('underscore')

Edit after camilo-martin's comment: All other posters talk about the bad pattern involved. So leaving that discussion aside, the best way to have a variable defined globally (OP's question) is through namespaces.

@tip: http://thanpol.as/javascript/development-using-namespaces/

查看更多
长期被迫恋爱
3楼-- · 2018-12-31 16:56

The other solutions that use the GLOBAL keyword are a nightmare to maintain/readability (+namespace pollution and bugs) when the project gets bigger. I've seen this mistake many times and had the hassle of fixing it.

Use a JS file then use module exports.

Example:

globals.js

var Globals = {
    'domain':'www.MrGlobal.com';
}

module.exports = Globals;

Then if you want to use these, use require.

var globals = require('globals'); //<< globals.js path
globals.domain //<< Domain.
查看更多
何处买醉
4楼-- · 2018-12-31 16:59

I agree that using the global/GLOBAL namespace for setting anything global is bad practice and don't use it at all in theory (in theory being the operative word). However (yes, the operative) I do use it for setting custom Error classes:

// Some global/config file that gets called in initialisation

global.MyError = [Function of MyError];

Yes, taboo here, but if your site/project uses custom errors throughout the place, you would basically need to define it everywhere, or atleast somewhere to:

  1. Define the Error class in the first place
  2. In the script where you're throwing it
  3. In the script where you're catching it

Defining my custom errors in the global namespace saves me the hassle of require'ing my customer error library. Imaging throwing a custom error where that custom error is undefined.

Also too, if this is wrong then please let me know as I've only just started doing this recently

查看更多
大哥的爱人
5楼-- · 2018-12-31 17:02

global._ = require('underscore')

查看更多
春风洒进眼中
6楼-- · 2018-12-31 17:05
global._ = require('underscore');

Note that using GLOBAL is deprecated:

(node:59502) DeprecationWarning: 'GLOBAL' is deprecated, use 'global'

My comment:

If you know what you're doing, I think its OK to use global. When I'd have to require some library every time I want to use it (and let's say I'm using it in tens or hundreds of files) I think it destroys the main point of a lot of libraries that are made to be easy and quick to use. Why is it named _, not amazingLibraryCalledUnderscore?

So for me, needing to write 30 chars of var _ = require('underscore'); is 3000% more than required minimum (1 char). Yes. I'm obsessed about making my life easier when writing the code. I just hate repeating obvious things. If I once told damn node what I mean by I want you to know _ means I use underscore I don't want to say it again.

So - I believe when you control your code, you should try to make it sexy. I believe writing 30 chars every time to do so simple thing is damn ugly!

ps. to trust yourself when you say I control my code, you need to have experience of loosing this control at least once.

查看更多
荒废的爱情
7楼-- · 2018-12-31 17:05

You can just use the global object.

var X = ['a', 'b', 'c'];
global.x = X;

console.log(x);
//['a', 'b', 'c']
查看更多
登录 后发表回答