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:

const someModule = require('/path/to/module.js');
    require(["moduleA", "moduleB"], function(A, B) {
       // do something with moduleA ...
       A.doSomething();
       // do something with moduleB ...
       B.doSomething();
    });
    head.js("/path/to/script.js");
    $LAB.script("/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"
3188 questions
1
vote
1 answer

Beginner on Ruby require syntax

A helpful article on here recommended to call Ruby gems explicitly even though they are built-in. So instead of require 'minitest/autorun' require_relative 'falcon'#file to test I wrote require 'rubygems' gem 'minitest' require…
enahel
  • 355
  • 3
  • 15
1
vote
1 answer

Use a returned value of a function from another file in PHP

I am using a function in a separate file, that gets should return me an array containing the enum values of a field:
Wilhelm Sorban
  • 1,012
  • 1
  • 17
  • 37
1
vote
1 answer

`require`ing files in a common codebase

I'm new to writing big Ruby projects (only mods and application scripting till now). I've made a project with some files in the project root and others in subfolders, including a "Test" folder. So far I've tried: Adding all folders and subfolders…
moshewe
  • 127
  • 1
  • 9
1
vote
1 answer

How to remove a script included with require after use in PHP?

I'm working with infinite cycles(daemons). Which include the same script for each iteration, the scripts are included with require function. I have a problem, scripts are magnified by each iteration, How could remove the script included after each…
1
vote
2 answers

How to organize require and require once in PHP for class import

I have built a PHP video upload library aimed to be used in different frameworks . ## HERE ##. But i want to make it optimized like , i dodnt want to include or make\ require or require_once call in class and also i want the configuration class to…
Develop4Life
  • 7,581
  • 8
  • 58
  • 76
1
vote
1 answer

Utilising Nodejs Promises (Bluebird) during module loading and configuration

Morning All I've been scouring S.O. for the past couple of days trying to find an answer I understood/was applicable to my situation, and have now admitted defeat, primarily due to my lack of comprehension around Promises (promise-n00b). So I'm…
nick-brown
  • 31
  • 2
  • 8
1
vote
1 answer

AssertionError: path must be a string is thrown when requiring own module

I wanted to create a very minified version of hapi-ninja and came across following problem: var settings = require('./app/server/config/settings'); var routes = require('./app/server/config/rout'); The first line works as it should an returns my…
HknLof
  • 111
  • 2
  • 12
1
vote
2 answers

php require_once not finding file with absolute path?

I just started setting up the google ads php library and right off the bat am having super frustrating troubles. I'm running Arch Linux, and I have all my files in /var/lib/googleads Trying to run GetRefreshToken.php, it requires a file called…
ryes31
  • 378
  • 1
  • 4
  • 15
1
vote
1 answer

node.js define own modules with pretty name

hey is it possible to define a module.export with a pretty name like require('my_module') that would help because i don't have to figure out all the '../../' i would have to use to get there. In my case its about the instance of my i18n helper. I…
antpaw
  • 15,444
  • 11
  • 59
  • 88
1
vote
2 answers

how to include php file contains 'use' command in another file with class

I have two files. in the first file (Facebook.php) i take user data via Facebook (Facebook access token):
giorgio83
  • 288
  • 2
  • 14
1
vote
0 answers

How to require Dagre in NodeJS

I'm trying to require Dagre in Node (following the documentation on the Dagre wiki) after installing Dagre with NPM. Why isn't this working? $ npm install dagre dagre@0.5.0 node_modules/dagre ├── graphlib@0.8.0 └── lodash@2.4.1 $ echo -e "var dagre…
1
vote
2 answers

Substitude one module for another in node.js

If I have a node.js application that has hundreds of files that reference a module (say underscore) and I want to replace that module with another (say lodash) then the obvious way to do this substitution would be a global name replace and switch…
Guy
  • 65,082
  • 97
  • 254
  • 325
1
vote
3 answers

Requiring Singleton Instances in Node

I spied in some code the following: var Log = require('log'); // This creates a singleton instance module.exports = new Log('info'); My first reaction was "no way that is a singleton", but I remember an article that outlined how Node.js does…
AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
1
vote
2 answers

How to require GSAP in Node.js

I try to include GSAP animation library in Node.js. For example I try to run GSAP with full features and to do that I need to include first 'TweenMax.js' and then next 'TimelineMax.js'. In last version on GSAP I see that 'TweenMax.js' and…
siropo
  • 210
  • 2
  • 14
1
vote
1 answer

Prevent tests from polluting required modules when running Mocha for multiple test files

I have a lib.js that I will require and test in 2 tests, test1.js and test2.js (using mocha and should.js): lib.js simply exports a data object: module.exports.data = {}; test1.js looks like this: var data = require('./lib.js').data; …
322896
  • 956
  • 1
  • 9
  • 19