Coder Social home page Coder Social logo

require-new's Introduction

DEPRECATED

Deprecated in favour of require-uncached.

For deprecation reason, see sindresorhus/import-fresh#4.

tl;dr; avoiding duplication of effort in the open-source world.

require-new

Travis build status NPM version

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

– http://nodejs.org/api/modules.html#modules_caching

require-new requires a new module object.

require-new does not affect the state or behavior of require method.

require-new has been designed to be used for module testing.

Usage

Load require-new module and use it to load a module just as you would with require:

var requireNew = require('require-new'),
    myModule = requireNew('my-module');

Example

If you have a module rand.js:

module.exports = Math.random();

Then requiring this module several times will result in the same response:

require('./rand.js'); // 0.697190385311842
require('./rand.js'); // 0.697190385311842

Modules are cached in a require.cache object when they are required.

require-new deletes the key value from the require.cache object associated with the module you are requesting, making the module reload:

requireNew('./rand.js'); // 0.2123227424453944
requireNew('./rand.js'); // 0.5403654584661126

It then restores the state of the require.cache object to ensure that it does not affect the behavior of require:

require('./rand.js'); // 0.48205413995310664
requireNew('./rand.js'); // 0.12475096038542688
requireNew('./rand.js'); // 0.2615479789674282
require('./rand.js'); // 0.48205413995310664

Alternative Design

You can export a function and call that function. This will make the module execute code multiple times.

module.exports = function () {
    return Math.random();
};
require('rand.js')(); // 0.561616780469194
require('rand.js')(); // 0.6468832329846919

See discussion on Stack Overflow to consider the pros and cons.

Download

Download using NPM:

npm install require-new

require-new's People

Contributors

gajus avatar

Stargazers

Kai avatar Vaughan Rouesnel avatar Laode Muhammad Al Fatih avatar Ken Huang avatar Josema avatar AMAGI / Jun Yuri avatar Darko Lukić avatar  avatar Valentin Semirulnik avatar Ari Palo avatar Paul Solbach avatar Michael Robinson avatar Raine Revere avatar Matt Cooney avatar Alberto Pose avatar Oscar Campbell avatar Roman Pearah avatar GP avatar  avatar BJR Matos avatar Bart van der Schoor avatar @racascou avatar Becky Conning avatar

Watchers

 avatar  avatar

require-new's Issues

Use ESHint

Remove "jshint" dev dependency from package.json and the gulp task.

sh:

npm install gulp-eslint --save-dev

gulpfile.js:

var eslint = require('gulp-eslint');

gulp.task('lint', function () {
    return gulp
        .src(['./src/*.js', './src/cinemas/*/*.js', './tests/*.js'])
        .pipe(eslint())
        .pipe(eslint.format())
        .pipe(eslint.failOnError());
});

.eslintrc:

{
    "rules": {
        "no-console": 0,
        "no-extra-parens": 2,
        "no-reserved-keys": 2,
        "no-eq-null": 2,
        "no-extend-native": 2,
        "no-process-env": 2,
        "no-self-compare": 2,
        "no-void": 2,
        "no-warning-comments": [1, { "terms": ["todo", "@toto"], "location": "start" }],
        "vars-on-top": 2,
        "wrap-iife": [2, "inside"],
        "global-strict": [2, "always"],
        "new-cap": 0,
        "no-shadow": 0,
        "no-mixed-requires": 0,
        "no-new-require": 2,
        "brace-style": [2, "1tbs"],
        "comma-style": [2, "last"],
        "func-style": [2, "expression"],
        "no-inline-comments": 2,
        "no-lonely-if": 2,
        "no-multiple-empty-lines": 2,
        "no-nested-ternary": 2,
        "one-var": 2,
        "operator-assignment": [2, "always"],
        "padded-blocks": [2, "never"],
        "quote-props": [2, "as-needed"],
        "quotes": [2, "single"],
        "space-after-keywords": [2, "always"],
        "space-before-blocks": [2, "always"],
        "space-in-brackets": [2, "never"],
        "space-in-parens": [2, "never"],
        "space-unary-ops": [2, { "words": true, "nonwords": false }],
        "spaced-line-comment": [2, "always"],
    },
    "env":{
        "mocha": true,
        "node": true
    }
}

resolve path incorrectly in terminal mode

I didn't try it in code, but I tried it in node terminal.

requireNew('./test') lead to the error:

  Error: Cannot find module '././test'
      at Function.Module._resolveFilename (module.js:336:15)
      at Function.require.resolve (module.js:388:19)
      at requireNew (/Users/zhaoyao-code/workspace/ydcy/pj-b/server/node_modules/require-new/src/require-new.js:21:26)
      at repl:1:1
      at REPLServer.defaultEval (repl.js:132:27)
      at bound (domain.js:254:14)
      at REPLServer.runBound [as eval] (domain.js:267:12)
      at REPLServer.<anonymous> (repl.js:279:12)
      at REPLServer.emit (events.js:107:17)
      at REPLServer.Interface._onLine (readline.js:214:10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.