Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tip: load source from a file in the Node.js shell

DZone's Guide to

Tip: load source from a file in the Node.js shell

· Performance Zone ·
Free Resource

xMatters delivers integration-driven collaboration that relays data between systems, while engaging the right people to proactively resolve issues. Read the Monitoring in a Connected Enterprise whitepaper and learn about 3 tools for resolving incidents quickly.

Node.js is really handy as an interactive shell for JavaScript. Alas, there is no simple command for loading source code from an external file. This post describes a work-around.

File test.js:

    exports.foo = function() {
        console.log("Hello world!");
    };
Interaction:
    > var t = require("./test");
    > t.foo()
    Hello world!
Node caches modules. Thus, if the source file changes, you need to clear the module cache and re-require the module:
    > module.moduleCache = {};
    > t = require("./test");

 

From http://www.2ality.com/2011/05/load-source-in-node.html

Discovering, responding to, and resolving incidents is a complex endeavor. Read this narrative to learn how you can do it quickly and effectively by connecting AppDynamics, Moogsoft and xMatters to create a monitoring toolchain.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}