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
161
votes
11 answers

How can I use jQuery in Greasemonkey scripts in Google Chrome?

As some of you may know, Google Chrome has put some severe limitation on Greasemonkey scripts. Chromium does not support @require, @resource, unsafeWindow, GM_registerMenuCommand, GM_setValue, or GM_getValue. Without require, I can't find a way to…
Alekc
  • 4,682
  • 6
  • 32
  • 35
156
votes
7 answers

Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all…
John Feminella
  • 303,634
  • 46
  • 339
  • 357
140
votes
8 answers

NodeJs : TypeError: require(...) is not a function

I am trying to require a file and afterwards pass it to a var. I am following this tutorial to create an authentication system. After writing the server.js file and trying to compile I got a BSON error therefore I changed the line that required the…
taigi100
  • 2,609
  • 4
  • 23
  • 42
136
votes
6 answers

require file as string

I'm using node + express and I am just wondering how I can import any file as a string. Lets say I have a txt file all I want is to load it into a variable as such. var string = require("words.txt"); I am against modules.exports = function(){ …
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
106
votes
10 answers

Conditional build based on environment using Webpack

I have some things for development - e.g mocks which I would like to not bloat my distributed build file with. In RequireJS you can pass a config in a plugin file and conditonally require things in based on that. For webpack there doesn't seem to be…
Dominic
  • 62,658
  • 20
  • 139
  • 163
103
votes
7 answers

Node-style require for in-browser javascript?

Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use as Node's require? To provide more detail: the reason require is so good is that it: Allows code to be dynamically loaded from other…
Alex Churchill
  • 4,887
  • 5
  • 30
  • 42
86
votes
2 answers

Node.js "require" function and parameters

When I do: lib = require('lib.js')(app) is app actually geting passed in? in lib.js: exports = module.exports = function(app){} Seems like no, since when I try to do more than just (app) and instead do: lib = require('lib.js')(app,…
user885355
  • 3,369
  • 4
  • 19
  • 10
82
votes
10 answers

electron 5.0.0 "Uncaught ReferenceError: require is not defined"

I had initially been using electron stable (4.x.x), and was able to use require in both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer…
junvar
  • 11,151
  • 2
  • 30
  • 46
82
votes
4 answers

RequireJS: Difference between "requirejs" and "require" functions

I am using requireJS 2.x. I found out that some tutorials (and the official docs) sometimes use requirejs.config({ [...] }); requirejs(["module"]) ... and sometimes require.config({ [...] }); require(["module"]) ... Is there any difference…
Matthias Bayer
  • 1,169
  • 1
  • 8
  • 15
81
votes
2 answers

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
  • 4,152
  • 6
  • 33
  • 54
79
votes
4 answers

webpack: import + module.exports in the same module caused error

I'm developing a website with webpack. When I have a code like this: import $ from 'jquery'; function foo() {}; module.exports = foo; I got the error Uncaught TypeError: Cannot assign to read only property 'exports' of object '#'. Turns out…
llanfair
  • 1,845
  • 4
  • 27
  • 43
78
votes
7 answers

How does require() in node.js work?

I tried this: // mod.js var a = 1; this.b = 2; exports.c = 3; // test.js var mod = require('./mod.js'); console.log(mod.a); // undefined console.log(mod.b); // 2 console.log(mod.c); // 3, so this === exports? So I image that require() may…
Trantor Liu
  • 8,770
  • 8
  • 44
  • 64
77
votes
2 answers

How are require, require_dependency and constants reloading related in Rails?

How are require and require_dependency different? How can require_dependency automatically reload classes in development but require can't ? I digged into Rails' ActiveSupport::Dependencies and dispatcher.rb code. What I saw in require_dependency's…
wei
  • 6,629
  • 7
  • 40
  • 52
74
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
  • 3,197
  • 3
  • 25
  • 42
70
votes
5 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,367
  • 1
  • 18
  • 28