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

ruby match regular expression

I'm trying to require all the files in a folder. I have the following. Dir.foreach File.expand_path("app/models") do |file| require file unless file === /^\./ end However, it's failing. When I open up a ruby console, I try the…
Hugo
  • 2,186
  • 8
  • 28
  • 44
1
vote
2 answers

Node require path reference issue

I am using node to run some JS tasks. I am using node require to include other needed files that I have written. The files I am trying to include exist in the same directory. There is nothing special about these files; just plain old JavaScript. …
Jordan Papaleo
  • 1,463
  • 11
  • 24
1
vote
1 answer

AngularJS and RequireJS - Uncaught Error: No module: MyApp

I am trying to create a new angular project with requireJS. here is my index.html:
Urbanleg
  • 6,252
  • 16
  • 76
  • 139
1
vote
1 answer

What does it mean when a Ruby script starts with Gem?

I am working on learning Ruby and one thing that I have seen in several instances and cannot understand are scripts that start with the keyword gem. An example can been seen in the Sensu code. gem "amqp", "1.3.0" require "amqp" require…
Chris Powell
  • 175
  • 3
  • 7
1
vote
1 answer

FPDF Fatal Error

I am trying to test implementation of FPDF. Below is the code I'm testing with, but it keeps giving me the error: "Fatal error: Class 'FPDF' not found in /home4/fwall/public_html/create-press-release.php on line 5". That is the URL to the page I am…
Eckstein
  • 785
  • 9
  • 27
1
vote
1 answer

Perl - Run script remotely through SSH that includes a require directive

Consider the following 2 scripts : main.pl #!/usr/bin/env perl require 'lib.pl'; testSub('Hello World'); lib.pl sub testSub { my ($input) = @_; print $input, "\n"; } 1; I need to run main.pl script remotely through SSH using sudo while…
iceman94
  • 129
  • 2
  • 10
1
vote
0 answers

"error reading from connection" on loading package on UNIX

I am working on a linux cluster. I installed required R packages in a local library created in my home directory. Till yesterday everything was fine. but suddenly today I am not able to load those packages in my local library. the error is "error…
Koundy
  • 5,265
  • 3
  • 24
  • 37
1
vote
0 answers

Redefinging global for a module in nodejs

I have a node module (IceJS) which insists on putting all of its exports, not in module.exports, but in global. In there a way to sandbox the module and take its global.ice and return it as a normal node module, as in var ice = require("Ice");?
Abex
  • 300
  • 5
  • 11
1
vote
2 answers

PHP requiring templates that use global variables

I'm trying to refactor some code and there are templates that use global variables. require and include inside of a function only uses local scope, so is there a way to "require global"? Right now, we have quite a few lines of code duplicated across…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
1
vote
1 answer

Whenever I use the word "require" I get an error and I also get an error if I try to use relative_require

I tried to solve my error by using relative_require, but this also does not seem to work. When I use the word require to run the following code: APP_ROOT = File.dirname(__FILE__) require File.join(APP_ROOT, 'lib') require 'guide' guide =…
pandasCat
  • 159
  • 2
  • 11
1
vote
2 answers

file.write not working from a different .lua

I wrote a lua function to add entered text to a users.txt in a specific manner, and that works fine when executed alone, but when i require it in main.lua it works- but never writes to users.txt. These are my two file's…
DivideByZero
  • 423
  • 6
  • 14
1
vote
1 answer

Ordering of require() seems to affect modules

I'm writing a very basic Node app with three components: index.js, mqtt.js, slack.js. The mqtt and slack modules both expose some of their methods with module.exports. However, I can only expose the files in one direction. Code sample: index.js: var…
kyleburke
  • 269
  • 4
  • 8
1
vote
1 answer

Best practice on avoid duplicated requires in nodejs

I have multiple js files, all have the same requires in the beginning like var config = require("config"); var expect = require("chai").expect; var commonAssertions = require('../../../utils/common_assertions.js'); var commonSteps =…
ccy
  • 1,385
  • 6
  • 17
  • 27
1
vote
1 answer

why does not PHP include work?

I have a strange problem. I have one file index.php in the root, and 2 files include directory inside the root. root > index.php , include include > config.php , db.php In include/db.php I defined MySQL connection parameters like this: …
Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127
1
vote
1 answer

Magento 1.8.1 Controller Override not working

I've been banging my head against something very very similar to this. I am trying to override a controller in my third party marketplace in Provider/Marketplace/controllers/SellerController.php As far as I can tell everything is set up properly in…
KRay
  • 71
  • 2
  • 10
1 2 3
99
100