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

PHP - One file or multiple (using require) - difference

I have one big PHP code divided into multiple files, each one is loaded with require (exactly one time for each). I'm curious if it's faster to have one big .php file with the whole code or different parts in different files (each part is always…
GerryMat
  • 13
  • 3
1
vote
0 answers

Require() works on localhost without parens but gives error in production

I have the code below, well a section of it that works perfectly on localhost
John Kariuki
  • 4,966
  • 5
  • 21
  • 30
1
vote
0 answers

Beginning ruby Peter Cooper 1.9 chapter 7 require load error

I get the error below when I require another file. I am running ruby ver. 2.0.0, although the book uses 1.9. I am wondering if this is version compatible problem, or gem issue? code in file: require 'string_extensions' puts "This is a…
user3905353
  • 77
  • 2
  • 12
1
vote
0 answers

node.js : can proxyquire be applied at any level in the call stack?

Node.js : can proxyquire be be used to mock dependencies at any level in the call stack ? I need to test route1.endpoint1 route1 requires module1 , which requires module2 , which requires module3 Can I apply proxyquire to module2 -- assigning a…
BaltoStar
  • 8,165
  • 17
  • 59
  • 91
1
vote
1 answer

Node.js import csv with blank fields

I'm trying to import & parse a CSV file using the csv-parse package, but having difficulty with requireing the csv file in the first place. When I do input = require('../../path-to-my-csv-file') I get an error due to consecutive commas because some…
Tyler
  • 11,272
  • 9
  • 65
  • 105
1
vote
1 answer

Can I use the 'Use' operator from a different PHP file?

I have this code here which will be used quite a lot throughout my app: require_once(APPPATH.'libraries/parse/autoload.php'); use Parse\ParseClient; use Parse\ParseObject; use Parse\ParseQuery; use Parse\ParseException; use Parse\ParseUser; use…
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
1
vote
1 answer

Architecture of folders with require/include

i have problem, i building my own CMS, where i using directories Main directory - index.php - /moduls - /functions /functions db-class.php /moduls/galerie /functions/galerie-class.php when i accesing out of folder galerie, it's showing me…
koca79331
  • 321
  • 4
  • 19
1
vote
1 answer

Cannot read property 'ajax' of undefined

Plz help me, I'm using the following code: index.js: var $ = require('./node_modules/jquery/dist/jquery.min'); var _ = require('./node_modules/underscore/underscore-min'); var Backbone =…
Vladi Isakov
  • 289
  • 6
  • 16
1
vote
1 answer

nodejs require is not loading object globally

I have two files in helpers folder, one is EventHelper.js and UserEvent.js, helpers/EventHelper.js function EventHelper() { this.onEventCreated = function(err, e) {...} this.isExisting = function(id) {...} } module.exports = new…
Srinivas
  • 553
  • 9
  • 21
1
vote
0 answers

Instantiate a class in another class code and get rid of include("class_name"), using namespace

so i have a class here that have a function who requires another class to create an object. I use namespace in both files, my question is can i get rid of this line here: include("class.php"); and instantiate class using namespace? here is the file…
Robert-Dan
  • 337
  • 2
  • 4
  • 13
1
vote
0 answers

Proper MVC in NodeJS

I'm figuring out different ways of doing things in node at the moment, and was wondering what would be the best way of passing required context to my Models and Controllers since I keep them as separate files. For example, this is my folder…
Reygoch
  • 1,204
  • 1
  • 11
  • 24
1
vote
2 answers

How can I write code that needs to run after some gem was loaded, but without requiring that gem?

I am writing a gem that "hooks" onto other gems. It's designed to work with multiple different gems that do similar things, this adds functionality onto any of them that may be present. What my gem does at startup is something like this: if…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
1
vote
2 answers

Include doesn't work?

I want to use a variable from a file in a other file but my include doesn't work. In my file index.php I have a switch: switch ($_POST["Jaartal"]) { case "2010": $dir= "2010"; break; case "2006": $dir= "2006"; …
mr13018
  • 17
  • 5
1
vote
1 answer

Node.js require caching

I am really stuck by nodejs cache system. I have this structure for my project : Project/ apps/ jobs_processor/ app.js processors.js processors/ libs/ queue_manager.js queue_manager.js…
Ismael
  • 320
  • 4
  • 15
1
vote
1 answer

Firefox addonsdk sandboxing and require API

I have a firefox addon-sdk. I created a test add-on similar to https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_started Now in my main.js I do something like this const { XMLHttpRequest } = require('sdk/net/xhr'); var fooLibrary =…