Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If, despite all the other answers, you still want to traditionally <em>include</em> a file in a node.js source file, you can use this:</p> <pre><code>var fs = require('fs'); // file is included here: eval(fs.readFileSync('tools.js')+''); </code></pre> <ul> <li>The empty string concatenation <code>+''</code> is necessary to get the file content as a string and not an object (you can also use <code>.toString()</code> if you prefer).</li> <li>The eval() can't be used inside a function and <em>must</em> be called inside the global scope otherwise no functions or variables will be accessible (i.e. you can't create a <code>include()</code> utility function or something like that).</li> </ul> <p>Please note that in most cases this is <em>bad practice</em> and you should instead <a href="http://nodejs.org/docs/latest/api/modules.html" rel="noreferrer">write a module</a>. However, there are rare situations, where pollution of your local context/namespace is what you really want.</p> <h3>Update 2015-08-06</h3> <p>Please also note this won't work with <code>"use strict";</code> (when you are in <a href="https://stackoverflow.com/q/8651415/688869">"strict mode"</a>) because functions and variables <em>defined</em> in the "imported" file <a href="https://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/" rel="noreferrer">can't be accessed</a> by the code that does the import. Strict mode enforces some rules defined by newer versions of the language standard. This may be another reason to <em>avoid</em> the solution described here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload