0
votes

I'm brand new to Node.js and Azure development so I'm likely doing something silly.

I've created a brand new Node.js website app in Azure. When I upload the following code, it works:

var express = require("express");
var app = express();

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.set('port', process.env.PORT || 8080);

app.get('/', function (req, res) {
    res.json({ test: "test" });
});

app.listen(app.get('port'));

But as soon as I add this line at the top:

var sql = require("mssql"); 

I start receiving 500 Internal Server Errors. The Azure logs show:

Fri Jun 23 2017 09:57:43 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated: SyntaxError: Use of const in strict mode. at Module._compile (module.js:434:25) at Object..js (module.js:464:10) at Module.load (module.js:353:31) at Function._load (module.js:311:12) at Module.require (module.js:359:17) at require (module.js:375:17) at Object. (D:\home\site\wwwroot\node_modules\mssql\index.js:1:80) at Module._compile (module.js:446:26) at Object..js (module.js:464:10) at Module.load (module.js:353:31)

The code doesn't throw errors locally.

I've done some googling and the only thing I can find is that this might be a problem with older versions of Node.js. I've tried adding that the node version to use should be greater than 6.0.0 in the packages.json, but this doesn't appear to do anything.

Please help!

1

1 Answers

1
votes

The message you are receiving is that you cannot use: var sql = require("mssql"); but const sql = require("mssql");

It's because you are using 'strict mode';. At the top of files, in javascript, you use 'use strict';

To learn more about ''use strict';` you can read here: https://www.w3schools.com/js/js_strict.asp