How do I view script scoped variables in Node.js - node.js

If I'm using the Node command line, I can look at global variables declared with var by displaying the global object, like so:
> global
The global variables are displayed as properties at the end of the global object.
If I declare a variable with let, then the variable is getting stashed in the "script scope" (I'm assuming; similar to what happens in the browser).
If I declare a top-level variable with let in the browser, I can view these script-scoped variables using the debugger.
Can I somehow look at script-scoped variables in Node?
Thank you.

If you want to see what you get from the globals you can do this.
In your code (index.js):
console.log(global)
Then you can save the output in a file from the terminal:
node index.js > global.json
OR
node index.js > global.txt

Related

How to call a variable from a preload script in godot?

When I make a preload script in godot and put a variable in there, how do I call the variable in my other scripts?
Is there a special way to call the variable that I don't know or is there a better way to do it than preload scripts?
If you are preloading a script, e.g. const item = preload("res://scripts/item.gd") it acts as a type. That is, you can declare variables as it, and make instances of it. The variables declared on the script exist on its instances:
const Item = preload("res://scripts/item.gd")
var my_item:Item
func _ready():
my_item = Item.new()
print(my_item.variable)
See Custom variable types and Classes and nodes. By the way, no, there are no static variables in Godot, see "static" on Keywords table.
You could be preloading a scene instead of a script. In that case you get a PackedScene, similar rules apply. But you would be using the instance method.
Instead of doing this, I suggest to give a class_name to your script. Godot will recognize it and make it available everywhere. See Register scripts as classes.
Please note that this is different from accessing a variable defined on another node in the scene tree. If you are trying to access a variable defined in the script of another node in the scene tree, the use get_node or similar to access the node, and then you can access the variable on it. See Understanding node paths.
If you need a global variable, what you want is an "singleton" autoload. You can set a scene to autoload in the "AutoLoad" tab in your project settings (select the scene path, give it a name, and click "Add"). They will be available on the scene tree regardless of the scene. They persist changes of scene.
Since autoloads are on the scene tree, you can use get_node to access them. The path will "/root/" followed by the name you gave it. For example:
onready var global_variables = get_node("/root/GlobalVariables")
func _ready():
print(global_variables.variable)
If you want to have access to one script from 1 or more different scripts, It is not a good way to use preload script. (at least in my opinion)
I prefer to make an script, global in the project and have access to that script from other scripts and nodes.
Now how to do that?
Open a new or a prefered scene.
On top menu, under project scroll, click on Project Settings.
On Project Settings, click on Autoload tab and add the script that you want to have access to from everyscript in the game and ofcourse add a fine Node Name to it.
Now based on that Node Name, you can have access to anything inside that global script.
For example, if this is my global script that I Autoloaded it as Global node name:
extends Node
var num: int = 5
I can have access to num variable from every other scripts like:
Global.num = 6
Make sure to take a look at the AutoLoad Documentation for more info.

Append a directory to Amazon Lambda Environment Variables $NODE_PATH

Default Env Variable as per the docs:
NODE_PATH:/opt/nodejs/node8/node_modules/:/opt/nodejs/node_modules:$LAMBDA_RUNTIME_DIR/node_modules
I want to append my custom directory to it (NOT to override all)
NODE_PATH:$NODE_PATH:/opt/nodejs/mycustom-directory
I tried the above from lambda console it overrides all. $NODE_PATH is added as a string. It is not parsing $NODE_PATH
Output I got when printing env:
NODE_PATH=$NODE_PATH:/opt/nodejs/mycustom-directory
Similar Question but no Solution still: AWS lambda add PATH variable?
I needed this too, as I am using AWS Lambda Layers, which are placed on the default NODE_PATH, and wanted to also be able to use local roots to avoid long relative paths (such as import bar from foo/bar instead of import bar from ../../../../foo/bar, but I didn't find any way to append to NODE_PATH without losing the default ones - as soon as it's set, the paths to node_modules - including the aws-sdk module, are lost.
The only solutions I can think of are:
Explicitly set NODE_PATH to the default value plus your custom one (which adds an ugly dependency to lambda internal environment configuration which you shouldn't have to care about)
Put your custom library in a layer. Many times this is a good solution if you can extract sub-modules as separate layers (but it doesn't help for situations like elimination of long relative paths within the application itself like I described above).
Append programatically, by having the very first lines of your application do
process.env.NODE_PATH = process.env.NODE_PATH + ":my-custom-path";
require("module").Module._initPaths(); // This re-initalizes the module loader to use the new NODE_PATH.
require('some-custom-module-in-my-custom-path'); // should work
require('aws-sdk') // should also work
This may not be the prettiest hack, but it should do the trick (disclaimer: haven't actually tried this in a AWS Lambda environment but it works locally using Node 12 at least).
I came up with the same problem.
I wrote a code to check if the lambda function applies the shell variable expansions, and to figure out they don't.
I set environment variable FOO as $NODE_PATH,
Then, run the check code (in lambda function):
const { FOO } = process.env;
exports.lambdaHandler = async (event, context, callback) => {
console.log(FOO);
};
The output is:
2019-02-22T08:29:05.714Z cde21239-628f-4a79-b046-6a14f177f59e $NODE_PATH
I just rewrite the whole NODE_PATH to be (my custom library path):/opt/nodejs/lib:/opt/nodejs/node8/node_modules/:/opt/nodejs/node_modules:/var/runtime/node_modules
the default value of NODE_PATH is explained there:
https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html

How does this example of passing environment variables in linux to node work?

I read and verified that I can pass environment variables to node by doing something like:
MY_ENV_VAR1=/tmp MY_ENV_VAR2=/data node index.js
How on earth does that work? I've only seen arguments to a script come after the script name, not before.
Thanks!
That is the standard way of defining and passing on the environment variables to a particular command from a Linux shell w/o exporting it.
More details: https://unix.stackexchange.com/questions/158117/how-to-pass-environment-variables-to-a-non-interactive-shell-with-example

Best practice when using an API key in Node.js

I have an API key I'm using in my Node.js application. Currently, I keep it stored in a text file and put it in a global variable when my application starts up.
So basically it's just:
var key = getKey();
useKeyGetData(key);
I don't like having this global variable, and it's a pain to pass between files. Is there a better way to get my key where/when I need it? Is there some standard for doing so?
The conventional alternative to what you're doing, especially when pertaining to API keys, is to use environment variables. This is an operating system-level configuration facility. Each process has its own set of environment variables, usually inherited from its parent process. By convention, environment variables have uppercase names.
In node.js, you can access environment variables through process.env. For example, if you run an application like this:
$ MY_VARIABLE=test node app.js
You can access the value of the MY_VARIABLE environment variable via:
process.env.MY_VARIABLE
It can be tedious, however, to have to keep passing the environment variable(s) on each invocation of your program. That's why there are packages such as dotenv which allow you to store your environment variables in a text file.
More specifically, you will have a file called .env and in it you might have:
MY_VARIABLE=test
OTHER_VARIABLE=foo
At the beginning of your app.js, you then do:
require('dotenv').config();
This reads the environment variable values from the .env file. You can then access them as you would access any other environment variables:
console.log("MY_VARIABLE: " + process.env.MY_VARIABLE);
console.log("OTHER_VARIABLE: " + process.env.OTHER_VARIABLE);
Now you don't have to explicitly pass the environment variables to your application upon invocation, i.e. you can just run it as usual:
$ node app.js
If you do pass one explicitly, it will override whatever value you gave in your .env file:
$ MY_VARIABLE=bar node app.js
Now the MY_VARIABLE environment variable will have a value of "bar" instead of "testing". Since OTHER_VARIABLE isn't passed explicitly, it retains its value of "foo" specified in the .env file.

Use puppet database variables in manifest

I am attempting to learn puppet, and so far so good.
What I am having an issue with is using a variable that I have set for a node or group in the web console.
I created a variable called myCustomSetting, and then in a manifest:
file {/var/tmp/myfile.txt
ensure => file,
content => $::myCustomSetting,
}
When I apply the manifest with puppet apply mytest.pp, there are no errors, but the file is empty? What am I missing?
The double semi colon indicate that you wish to reach the top level scope, where you supposedly declared your variable. Check that your variable has not been declared in a local scope.

Resources