Totty.js
Totty.js

Reputation: 15831

How to use require-js on node.js without having to require("requirejs") and redefine requirejs in each file?

I'm trying to use requirejs in node.js but I don't like to redefine the requirejs in each file.

Upvotes: 0

Views: 903

Answers (2)

C Blanchard
C Blanchard

Reputation: 1063

I'm only able to see two options to import modules into your files without reusing require. Global variables or Dependency Injection. Links contain the examples

Although both techniques can be useful, neither are particularly good options in this scenario. Using global variables makes your code hard to debug and reduce readability. Dependency injection will clutter up your app far more than reusing require.

The method you're using now seems to be the current common practice.

Requiring the same module over and over again isn't necessarily a bad thing. You don't need to worry about a performance hit, since modules are cached after they're first loaded and only executed once. It also makes your code more readable, since you know upfront which libraries you're using.

Upvotes: 0

matth
matth

Reputation: 6289

You can use node.js global variables

GLOBAL.example = require('./example');

Upvotes: 1

Related Questions