Over a million developers have joined DZone.

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

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.

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!");
    > 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

See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}