joshua.paling
joshua.paling

Reputation: 13952

Every JS file throws error until it's opened and saved on Azure

I'm deploying a node app to Azure, using local git deployment. I have it working without issues in the staging environment (also on Azure) and I'm now setting up a production environment.

Every single file involved in the app throws an error - but as soon as I open that file in my FTP client and just save it, without making changes, the error for that particular file goes away - and the next file used throws an error.

So the steps I took are something like this:

Run deployment, refresh browser.

Get error like Unexpected token ILLEGAL on server.js line 1

Save server.js in FTP client, without making changes.

Restart app, refresh browser.

server.js now has no issues, but the first file it requires, express, gives an error cannot find module ./lib/express on node_modules/express/index.js:11 (./lib/express is definitely there)

Save node_modules/express/index.js:11 in FTP client, without making changes.

Restart app, refresh browser.

Now, node_modules/express/index.js has no issues, but the first file it requires, ./lib/express will then give an error: cannot find module merge-descriptors on node_modules/lib/express.js:16

I'll stop there, but in real life I continued and the behaviour is consistently ongoing - each file errors on the first thing it tries to require, until it's been saved in the FTP client.

To top it all off, I left the app unchanged for 20 minutes, came back, and I was back to the beginning - with an Unexpected token ILLEGAL on server.js line 1 despite NO changes to my code. I tried saving each file and basically just repeated the steps above, getting the same results.

I'm completely stuck and have no idea what to do next short of saving every single file in the codebase. Any idea what could be going on here, or how I could move forwards with debugging the issue?

Upvotes: 0

Views: 114

Answers (2)

joshua.paling
joshua.paling

Reputation: 13952

After being in touch with Azure support, it turns out the issue was due to WEBSITE_DYNAMIC_CACHE being set to 1, not 0. The feature isn't visible in the azure portal, and is "in development and is currently in private preview." The issue I came across is a known bug with WEBSITE_DYNAMIC_CACHE and node apps.

We're still not sure how / why it got set to 1 in the first place, but it's fixed, so we don't care for now.

What a fun day!

Upvotes: 0

duncanhall
duncanhall

Reputation: 11431

You most likely have a Byte-Order-Mark at the beginning of your files.

There is a simple gist by Domenic Denicola that shows how you can detect and remove this for all files in the current directory:

var fs = require("fs");
var path = require("path");
var wrench = require("wrench");

var BOM_CHAR_CODE = 65279;
var BOM = String.fromCharCode(BOM_CHAR_CODE);

var fileNames = wrench.readdirSyncRecursive(process.cwd()).filter(function (fileName) {
    return path.extname(fileName) === ".js";
});

fileNames.forEach(function (fileName) {
    fs.readFile(fileName, "utf8", function (err, fileContents) {
        if (err) { throw err; }

        if (fileContents.charCodeAt(0) !== BOM_CHAR_CODE) {
            fs.writeFile(fileName, BOM + fileContents, "utf8", function (err) {
                if (err) { throw err; }
            });
        }
    });
});

Upvotes: 1

Related Questions