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
70
votes
5 answers

How to remove module after "require" in node.js?

Let say, after I require a module and do something as below: var b = require('./b.js'); --- do something with b --- Then I want to take away module b (i.e. clean up the cache). how I can do it? The reason is that I want to dynamically load/ remove…
murvinlai
  • 48,919
  • 52
  • 129
  • 177
69
votes
3 answers

NodeJs require('./file.js') issues

I am having issues including files to execute in my NodeJs project. I have two files in the same directory: a.js var test = "Hello World"; and b.js require('./a.js'); console.log(test); I execute b.js with node b.js and get the error…
Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74
65
votes
6 answers

What are the paths that "require" looks up by default?

In Ruby, I have been told that when doing require "some_file" Ruby will look for the file in certain places. I know that it looks for some_file.rb, but where does it look for it by default?
Mark Provan
  • 1,041
  • 2
  • 13
  • 19
65
votes
11 answers

React Native: require() with Dynamic String?

I have read several posts about issues that people are having with React Native and the require() function when trying to require a dynamic resource such as: Dynamic (fails): urlName = "sampleData.json"; data = require('../' + urlName); vs. Static…
Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
62
votes
7 answers

Is there a pluralize function in Ruby NOT Rails?

I am writing some Ruby code, not Rails, and I need to handle something like this: found 1 match found 2 matches I have Rails installed so maybe I might be able to add a require clause at the top of the script, but does anyone know of a RUBY method…
aarona
  • 35,986
  • 41
  • 138
  • 186
60
votes
8 answers

How can I require an optional Perl module if installed?

I have Perl code which relies on Term::ReadKey to get the terminal width. My installation is missing this module, so I want to provide a default if the module isn't present rather than throw an exception. How can I conditionally use an optional…
dlamblin
  • 43,965
  • 20
  • 101
  • 140
57
votes
4 answers

Is there a shorter way to require a file in the same directory in ruby?

Is there a shorter way to require a file located in the same directory (as the script being executed)? require File.expand_path(File.dirname(__FILE__) + '/some_other_script') I read that require "my_script" and require "./my_script" will actually…
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
57
votes
2 answers

Node.js - check if module is installed without actually requiring it

I need to check whether "mocha" is installed, before running it. I came up with the following code: try { var mocha = require("mocha"); } catch(e) { console.error(e.message); console.error("Mocha is probably not found. Try running `npm…
AndreyM
  • 1,403
  • 2
  • 12
  • 23
56
votes
5 answers

Load Lua-files by relative path

If I have a file structure like this: ./main.lua ./mylib/mylib.lua ./mylib/mylib-utils.lua ./mylib/mylib-helpers.lua ./mylib/mylib-other-stuff.lua From main.lua the file mylib.lua can be loaded with full path require('mylib.mylib'). But inside…
RocketR
  • 3,626
  • 2
  • 25
  • 38
56
votes
4 answers

Disabling warning about "require" function in JSHint

I'm writing some code for Node.js and I'm currently using JSHint to check over my code. However, when I use the require function to import modules, it says: 'require' is not defined. How can I suppress the warning? "use strict"; var express =…
somesh
  • 589
  • 1
  • 5
  • 11
54
votes
1 answer

Webpack and external libraries

I’m trying out webpack (http://webpack.github.io/) and it looks really nice, however I’m kind of stuck here. Say that I’m using a CDN for a library, f.ex jQuery. Then in my code, I want the require('jquery') to automatically point to the global…
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
52
votes
4 answers

Is require File.expand_path(..., __FILE__) the best practice?

Is require File.expand_path(..., __FILE__) the best way to require other files within a project?
dan
  • 43,914
  • 47
  • 153
  • 254
52
votes
6 answers

Check if an include (or require) exists

How do you check if an include / require_once exists before you call it, I tried putting it in an error block, but PHP didn't like that. I think file_exists() would work with some effort, however that would require the whole file path, and a…
CafeHey
  • 5,699
  • 19
  • 82
  • 145
47
votes
2 answers

How do I rescue from a `require': no such file to load in ruby?

I am trying to rescue from a ``require': no such file to load in ruby` in order to hint the user at specifying the -I flag in case he has forgotten to do so. Basically the code looks like: begin require 'someFile.rb' rescue puts "someFile.rb was…
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
47
votes
6 answers

PHPStorm: undefined variables caused by include/require

PHPStorm showed that all the variables from other files, both required and included, are undefined. I found this solution here, but after I disabled that option Ignore 'include' and 'require' statements, the IDE ignored all undefined variables. For…
kxc
  • 1,357
  • 2
  • 16
  • 39