Good day,
I am trying to read a variable from a process. With javascript, I know, you can use console.log(window['variable-name']) to find a variable's contents using a string.
However, for NodeJs, I know you can use console.log(global['variable-name']), but the variable needs to be global, how can I read module local variables?
Short answer: only by exporting them (module.exports.your_var = your_var) or by setting the variable on global (global.your_var = your_var).
Longer answer:
Modules, when loaded by Node.js, are being wrapped within a Javascript function and then called when required the first time. Function variables belong to the function's scope and they are unaccessible from outside.
Related
I've changed the value of the variable defined in my .gitlab-ci.yml, but I've noticed, that the old value is still used.
After a few retries I've checked the global configuration to find out, that the variable with the same name is defined in the global CI settings, and my pipeline is not taking the 'old' value defined in .gitlab-ci.yml, but that defined in the global scope.
Is there any way to override the globally defined variables, or at least get warned when the global value gets the precedence?
Some commands are very strict about what variables they use, and if someone defined the global defaults, that need to be changed in my case, I have a little problem here.
I am using InstallShield Express to create a setup project.
I try to add a custom action for Uninstallation, before "System Changes".
The custom action is a JavaScript which will open a window, as below:
window.open("https://www.example.com/", "_blank");
However, when I try to uninstall the program, I get an error said "Microsoft JScript Runtime Error, 'window' is not defined.
Why?
Update:
Finally I choose to use a MSI DLL instead of the script to solve the problem. What should I do with this question? THanks.
The windows object does not exist in NodeJS. However, if you just want to declare a global variable to use it later, you can add the open method in the global object like:
glboal.open = function whatever(url, param2){
// does the stuff you want
};
And then you can use it this way:
conosle.log(global.open('https://www.example.com/','_blank'));
But i still do not recommend creating a global variable for this. Make a function in a file and import it when you need it. Global variables can cause coupling, and make code harder to read. Also, this variable will exist the entire lifetime of the application, and this might not be good if you are going to make more of them.
If you want to read more about why global variables are bad: https://wiki.c2.com/?GlobalVariablesAreBad
I have a function block in my Blockly workspace, but I can't seem to find a way to add a variable that is scoped to a function. Adding a variable to the workspace generates a variable defined at the top of my generated code.
Is there a way with the pre-built blocks to create a variable within a function block?
Unfortunately, not really; all of the variables in Blockly are global. One thing you can do is have the function block in some way know which variables it's using and then redefine it with 'let' inside the function such that it won't be modified outside of the function, but variable selectors will still be able to select the variable regardless of location.
Is there a way to find all the places across a project where a particular function is invoked? In particular, taking into account any times that the file is imported.
For example, if we're looking for where library.js's function foobar() is used, this example in another file would be included:
var library = require('./myfile.js')
library.foobar()
(The reason simply searching for foobar across all files won't work is if the function is a common name used in multiple modules.)
This would be the opposite of something like Webstorm's Jump to Definition feature.
One of the use-cases is to help refactor a legacy codebase where it's unclear where certain exported functions are being used, or if they're being used at all.
I saw similar questions like this one, but these seem to be for client-side javascript and don't take exports into account.
You can add
console.trace(); inside your function and you will know where does it called.
Sometime I use callsite module in the function I want to check for log the absolute link to file use this function.
I was wondering if there was a way to list all scope variables (and possibly outer scopes as well) in the built in node.js debugger. So far I've managed to access the global scope using:
debug> repl
> global
but is there a function that lists local scope variables or a pointer to the local scope?
See the debugger commands and global object for more info.
no, currently it's not implemented in built in cli debugger. If you really need it I encourage you to try and add support yourself - the code is in /lib/_debugger.js and command is "scope". It's doable in less than 50 lines of code