I've been publishing a NodeJs Express web app on Azure as an experiment. It was working great when I used ES6 / ESM syntax only in the JS that the browser used.
Since I prefer ES6 syntax, I reworked my server-end JS to use ES6 classes and ESM import syntax, including changes to the package.json (type = module) and to the Express-provided files.
All of that works great when I run it locally (WebStorm on a Mac). I am running NodeJs 12.18.2.
When I publish the app to Azure, though, it chokes. I get a blank page when I go to the URL. It turns out that the app doesn't start and so no page.
The error message suggests that only on Azure, NodeJs just doesn't get the whole module idea:
import express from "express";
^^^^^^
SyntaxError: Cannot use import statement outside a module
Again, I only see this on Azure. Azure is running NodeJs 12.13.0. I'm using Kudu as the build process.
On my machine it runs beautifully, because I have the root package.json file set up correctly (it seems):
{
"name": "--redacted--",
"type": "module",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www.js"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"esm": "^3.2.25",
"express": "~4.16.1",
"morgan": "~1.9.1"
}
}
But the package.json file is identical on Azure, though, and it doesn't prevent the fail.
Here's the beginning of the code that's failing at the top of app.js, although the error is generic to any use of import:
import express from "express";
import path from "path";
import cookieParser from "cookie-parser";
So, what am I doing wrong? Syntax looks right and works at home, but fails on Azure. Is it a config / dependency / NodeJs version issue?
I've already tried things like using the esm package or not using it (I don't need it locally), changing the start script in package.json to include -r esm or to exclude it (it isn't needed in my local runs), and to use the deprecated --experimental-modules, and so on.
Does anyone know why it doesn't work on Azure even though it does locally? Please help me out!