Can I require a function in node.js without executing the entire required file?
I want to know if there's a way to require a function without executing all the required file.
No, you cannot. With CommonJS modules, the only way you know what is exported is by running the file and letting it assign things to module.exports
. Without running the file, there would be nothing exported.
The design solution for this would be to move the exported function you want into a separate module by itself where that module does nothing except export your function. Then, you can require()
that module without any side effects of loading the module.
Require file without executing code?
Is it possible to load a file without having it to run the code?
No, everything in a ruby file is executable code, including class and method definitions (you can see this when you try to define a method inside an if-statement for example, which works just fine). So if you wouldn't execute anything in the file, nothing would be defined.
You can however tell ruby that certain code shall only execute if the file is run directly - not if it is required. For this simply put the code in question inside an if __FILE__ == $0
block. So for your example, this would work:
file.rb
def method
puts "This won't be outputted."
end
if __FILE__ == $0
puts "This will not be outputted."
end
main.rb
require "./file"
Using require without export
Could someone explain what is happening with this code? In other words, how does require work when not used with export.
We almost always see require()
being used with module.exports
, but you don't have to. When you don't export anything, the code in the imported module will still run, but you can't bind the import to a variable and interact with it.
Consider the following Foo.js
module :
var foo = {};
foo.greet = function(){
console.log('Hello from Foo!');
}
foo.greet();
I can import this module in my main file, like so :
require('./foo');
If I run this main file, the code inside the Foo.js
module will run, and Hello from Foo! will be printed to the console.
However, I can't interact with the foo object directly. The following code will not work :
require('./foo');
foo.greet(); //ReferenceError, foo is not defined
I can bind the module import to a variable, but even this will not work :
var foo = require('./foo');
foo.greet(); //TypeError, foo.greet is not a function
To get it to work, I need to export the foo object from my module, using the module.exports
that you are familiar with.
This demonstrates that you don't need to export anything from your modules, just like you don't need to bind the imported module to a variable when you are requiring it. The difference is that you won't be able to interact with the code in the imported module, if you don't export what you don't want to make visible in that module.
In the code in your question, importing Redis works because that module is self-contained, you don't need to interact with it in your code. You only need to import the code so that it can run (require the main Redis module and create the client)
How to execute an included/required PHP file without having to make calls to it's functions or use auto_prepend_file ?
If you do a require_once and you put your code in the body of the required document it will be executed when you do the require_once.
If you have your code in functions then you can call them in the required document:
<?php
function DoSomething()
{
//Do something :D
}
DoSomething();
?>
Why is Python running my module when I import it, and how do I stop it?
Because this is just how Python works - keywords such as class
and def
are not declarations. Instead, they are real live statements which are executed. If they were not executed your module would be empty.
The idiomatic approach is:
# stuff to run always here such as class/def
def main():
pass
if __name__ == "__main__":
# stuff only to run when not called via 'import' here
main()
It does require source control over the module being imported, however.
Related Topics
Approach for Installing System Service Implemented as Ruby Gem
How to Read Post Data in Rack Request
Why Does Psych Yaml Interpreter Add Line Breaks Around 80 Characters
Rails Include Only Selected Columns from Relation
Check If the Sum of Two Different Numbers in an Array Equal a Variable Number
Append Row to CSV File Ruby 1.9 CSV Lib
How to Make a Custom Environment in Rails a Default Environment
Benefits of Using 'Hash#Fetch' Over 'Hash#[]'
Made a Mistake Installing Rvm with Sudo. How to Reverse
How to Break Out of a Map/Collect and Return Whatever Has Been Collected Up to That Point
Get Time from Datetime Variable in Ruby
How to Get Ruby-Debug-Ide to Work
Understand "Current_User" Concept When Creating a Login Session in Ruby
Get Available Diskspace in Ruby
How to Have One Resource in Routes for Namespace and Root Path Altogether - Rails 4
Use Same Controller for Multiple Routes
Error Connecting to Redis on 127.0.0.1:6379 (Errno::Econnrefused) - Wercker