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
1424
votes
28 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
  • 38,833
  • 65
  • 160
  • 266
556
votes
8 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
  • 5,907
  • 6
  • 21
  • 25
519
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
  • 22,247
  • 13
  • 42
  • 47
391
votes
8 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
  • 85,281
  • 83
  • 234
  • 341
381
votes
13 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,538
  • 2
  • 27
  • 22
355
votes
10 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…
Corno
  • 5,448
  • 4
  • 25
  • 41
332
votes
9 answers

What is the difference between require_relative and require in Ruby?

What is the difference between require_relative and require in Ruby?
ab217
  • 16,900
  • 25
  • 74
  • 92
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,927
  • 7
  • 25
  • 28
276
votes
40 answers

How can I 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 Express.js' app.js line 6, you will see express = require('../../') That's really bad, IMO. Imagine I would like…
Totty.js
  • 15,563
  • 31
  • 103
  • 175
264
votes
21 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
  • 3,193
  • 4
  • 20
  • 22
228
votes
11 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,888
  • 6
  • 55
  • 76
226
votes
16 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 below: a.js (the main file run with node) var ClassB = require("./b"); var ClassA =…
Runcible
  • 3,008
  • 3
  • 19
  • 19
224
votes
21 answers

Cannot redeclare block scoped variable

I'm building a node app, and inside each file in .js used to doing this to require in various packages. let co = require("co"); But getting etc. So using typescript it seems there can only be one such declaration/require across the whole project?…
dcsan
  • 11,333
  • 15
  • 77
  • 118
192
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
  • 7,684
  • 12
  • 41
  • 56
176
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,829
  • 13
  • 73
  • 134
1
2 3
99 100