Search code examples
node.jsnode-webkitejsembedded-javascript

How does one use ejs with node-webkit


in package.json

{
  "name": "_dubs",
    "version": "0.0.0",
    "description": "GAME",
    "main": "views/index.ejs",
    ...

the app gives me this error "SyntaxError: unexpected token <"

Which I assume to be an issue due to ejs! (embedded js)

Any idea on how I can get around this?


Solution

  • You can't use anything other than pure html5 as your main file as far as node-webkit is concerned. You would have to use the ejs module to first compile the content you want to appear in your document, then place said content into the document as you wish.

    Here's an example that might help:

    index.html

    <html>
    <head>
        <script>
            var fs = require('fs');
            var ejs = require('ejs');
    
            fs.readFile('./index.ejs', function(err, data){
                if(err){
                    document.body.innerHTML = err.toString();
                    return;
                }
    
                var template = ejs.compile(data.toString());
                document.body.innerHTML = template({ name: 'world' });
            });
        </script>
    </head>
    <body>
        Not yet rendered...
    </body>
    </html>
    

    index.ejs

    <div>hello, <%= name %>!</div>
    

    Make sure that you have ejs installed as a dependency through npm, then when you run this, your body should then contain:

    <div>hello, world!</div>