Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Client and server <code>JavaScript</code> (or <code>CoffeeScript</code>) works differently. So, its a really difficult to write a module that'll work in both applications.</p> <p>There is a lot of libraries to solve this problem, like <a href="http://requirejs.org/" rel="nofollow">RequireJS</a> and <a href="http://browserify.org/" rel="nofollow">Browserify</a>.</p> <p>But I have two simpler suggestions for your problem.</p> <hr> <p>First one is to use <code>JSON</code> to store your global constants. On the server side you can simply <code>require</code> you <code>JSON</code> file:</p> <pre><code>root = require './config.json' </code></pre> <p>On the client side you may either parse it manually or serve it as <code>pjson</code>.</p> <hr> <p>My second suggestion is to write really simple module that'll be compatible with both your applications. It'll look something like this:</p> <pre><code>root = myVariable: 300 myOtherVariable: 400 modulte.exports = root if module?.parent? </code></pre> <p>This code should be compatible with both <code>node.js</code> <code>require</code> function and browser <code>&lt;script&gt;</code> tag.</p> <h1>Update:</h1> <p>I just reread you question and realized, that you've did almost as I suggested. But your code looks fine to me. You may try to use <code>module.export</code> instead of its alias <code>exports</code>, it may help:</p> <pre><code>root = modulte?.exports ? this root.myVariable = 300 </code></pre> <p>But, as I said, your code looks fine to me as well.</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. 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