Questions tagged [require]

In interpreted languages like Lua, PHP and Ruby, "require" is a statement that tells the interpreter to include a certain source-file at that position where the "require" statement has been placed.

Require in PHP

In PHP there exist the statements include() and require() to include another file with PHP-code into your document.
The difference to include is, that require will throw an error if the file is not available.

A variant of require() is require_once(). This includes a file only, if it hasn't already been included before.

require('/path/to/script.php');

Require in Ruby

In Ruby, the require method does what include does in most other programming languages: It will include and run another file.

It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method.

require '/path/to/script.rb'

The include and require methods do very different things. Please check the source for further information on include.

Source: http://ruby.about.com/b/2008/10/23/a-quick-peek-at-ruby-include-vs-require.htm

Require in JavaScript

JavaScript has no native support for the require statement. However, there exist various implementations that try to mimic the behaviour of other programming languages.

These implementations are not fixed to use the word "require" for their loading routines, but may use other statements.

Some of them are:

  • RequireJS

    require(["moduleA", "moduleB"], function(A, B) {
       // do something with moduleA ...
       A.doSomething();
       // do something with moduleB ...
       B.doSomething();
    });
    
  • Head JS

    head.js("/path/to/script.js");
    
  • LABjs

    $LAB.script("/path/to/script.js");
    
  • jQuery Plugin

    jQuery.require("/path/to/script.js");
    

Require in Lua

The require statement of Lua searches for a module with the given name automatically extended by a previously defined path pattern.

The path patterns are rules that specify how to construct a path name using the parameter given to require. After path construction, require will check if one of the constructed paths is valid in order to load this path.

The pattern usually includes an expression, which extends the given name by '.lua', Lua caches already loaded modules, so they won't be loaded twice.

    require "module"
2940 questions
1302
votes
25 answers

Difference between require, include, require_once and include_once?

In PHP: When should I use require vs. include? When should I use require_once vs. include_once?
Scott B
  • 35,095
  • 61
  • 148
  • 245
484
votes
11 answers

What is the difference between include and require in Ruby?

My question is similar to "What is the difference between include and extend in Ruby?". What's the difference between require and include in Ruby? If I just want to use the methods from a module in my class, should I require it or include it?
Owen
  • 19,917
  • 13
  • 38
  • 47
364
votes
11 answers

Best way to require all files from a directory in ruby?

What's the best way to require all files from a directory in ruby ?
Gaetan Dubar
  • 4,358
  • 2
  • 24
  • 22
353
votes
14 answers

node.js require all files in a folder?

How do I require all files in a folder in node.js? need something like: files.forEach(function (v,k){ // require routes require('./routes/'+v); }};
Harry
  • 47,045
  • 66
  • 163
  • 243
345
votes
7 answers

Detect if called through require or directly by command line

How can I detect whether my Node.JS file was called using SH:node path-to-file or JS:require('path-to-file')? This is the Node.JS equivalent to my previous question in Perl: How can I run my Perl script only if it wasn't loaded with require?
700 Software
  • 77,509
  • 74
  • 213
  • 324
322
votes
5 answers

The difference between "require(x)" and "import x"

I've just started working on a small node project that will interface with a MongoDB. However, I cannot seem to get the relevant node modules to import correctly, even though I have installed them correctly via npm. For example, the following code…
austinthemassive
  • 3,549
  • 5
  • 17
  • 25
317
votes
5 answers

When should I use require() and when to use define()?

I have being playing around with requirejs for the last few days. I am trying to understand the differences between define and require. Define seems to allow for module separation and allow for dependency ordering to be adhere. But it downloads…
skinnybrit51
  • 4,847
  • 7
  • 22
  • 28
309
votes
7 answers

What is the difference between require_relative and require in Ruby?

What is the difference between require_relative and require in Ruby?
agentbanks217
  • 15,794
  • 24
  • 66
  • 89
286
votes
9 answers

is there a require for json in node.js

I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file. If I wanted to include another JavaScript file I could simply use require. Now I'm using readFileSync and __dirname to…
Coen
  • 4,586
  • 4
  • 21
  • 38
250
votes
37 answers

How to make node.js require absolute? (instead of relative)

I would like to require my files always by the root of my project and not relative to the current module. For example if you look at https://github.com/visionmedia/express/blob/2820f2227de0229c5d7f28009aa432f9f3a7b5f9/examples/downloads/app.js line…
Totty.js
  • 14,070
  • 25
  • 93
  • 165
247
votes
19 answers

Nodejs cannot find installed module on Windows

I am learning nodejs at the moment on Windows. Several modules are installed globally with npm.cmd, and nodejs failed to find the installed modules. Take jade for example, npm install jade -g Jade is installed in directory "C:\Program Files…
Cosmore
  • 2,941
  • 4
  • 16
  • 21
189
votes
7 answers

PHP - Failed to open stream : No such file or directory

In PHP scripts, whether calling include(), require(), fopen(), or their derivatives such as include_once, require_once, or even, move_uploaded_file(), one often runs into an error or warning: Failed to open stream : No such file or…
Vic Seedoubleyew
  • 9,705
  • 6
  • 40
  • 61
182
votes
15 answers

How to deal with cyclic dependencies in Node.js

I've been working with nodejs lately and still getting to grips with the module system so apologies if this is an obvious question. I want code roughly like the following below: a.js (the main file run with node) var ClassB = require("./b"); var…
Runcible
  • 2,498
  • 3
  • 17
  • 18
175
votes
13 answers

Ruby 'require' error: cannot load such file

I've one file, main.rb with the following content: require "tokenizer.rb" The tokenizer.rb file is in the same directory and its content is: class Tokenizer def self.tokenize(string) return string.split(" ") end end If i try to run…
The Coding Monk
  • 6,777
  • 11
  • 38
  • 51
175
votes
7 answers

Difference between "include" and "require" in php

Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?
Dan Hanly
  • 7,621
  • 12
  • 67
  • 127
1
2 3
99 100