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:

  • RequireJS

    require(["moduleA", "moduleB"], function(A, B) {
       // do something with moduleA ...
       A.doSomething();
       // do something with moduleB ...
       B.doSomething();
    });
    
  • Head JS

    head.js("/path/to/script.js");
    
  • LABjs

    $LAB.script("/path/to/script.js");
    
  • jQuery Plugin

    jQuery.require("/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"
2940 questions
61
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
  • 43,517
  • 50
  • 120
  • 169
60
votes
3 answers

node.js require cannot find custom module

Here is the project structure: / app.js package.json /node_modules /app config.json /frontend assets and html tpls /modules couch.js raeume.js users.js I require config.json, raeume.js and users.js from…
thgie
  • 2,230
  • 1
  • 17
  • 26
59
votes
6 answers

Load node.js module from string in memory

How would I require() a file if I had the file's contents as a string in memory, without writing it out to disk? Here's an example: // Load the file as a string var strFileContents = fs.readFileSync( "./myUnalteredModule.js", 'utf8' ); // Do some…
ZECTBynmo
  • 2,838
  • 2
  • 20
  • 33
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
  • 40,659
  • 30
  • 140
  • 167
56
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
  • 32,176
  • 39
  • 119
  • 171
55
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
  • 40,676
  • 19
  • 92
  • 127
55
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
  • 579
  • 1
  • 5
  • 11
53
votes
1 answer

ES6 import equivalent of require() without exports

By using require(./filename) I can include and execute the code inside filename without any export defined inside filename itself. What is the equivalent in ES6 using import ? Thanks
crash
  • 3,060
  • 4
  • 22
  • 41
51
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,333
  • 2
  • 11
  • 23
50
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
  • 39,098
  • 40
  • 137
  • 234
50
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
  • 97,234
  • 40
  • 163
  • 203
50
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…
MintDeparture
  • 5,745
  • 15
  • 76
  • 136
46
votes
4 answers

How do I change the order in which Meteor loads Javascript files?

When you make a project with the Meteor framework, it packages all the files together, but there doesn't seem to be a way to explicitly say "I want this file to be loaded before that one". Let's say, for example, I have 2 javascript files: foo.js…
Jeremyfa
  • 662
  • 1
  • 7
  • 12
44
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
  • 35,550
  • 26
  • 140
  • 232
43
votes
4 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,321
  • 2
  • 20
  • 34