Search code examples
node.jsazureexpressiisnodenext.js

Issue running Next.js on iisnode on Azure Web App


Trying to deploy and run Next.js on Azure Web App. Azure Web App works when running just with Express.js but as soon as I call nex() it fails. Tried to enable the error logging in Azure portal but not much of any use came out, just generic 500 errors.

Below is what works and what doesn't.

Works:

var express = require('express');
var expressServer = express();

expressServer.get('/', function (req, res) {
    res.send('Express is working on IISNode!');
});

expressServer.listen(process.env.PORT || 8080);

Does not work:

var express = require('express');
const next = require('next');
var expressServer = express();
var app = next();

expressServer.get('/', function (req, res) {
    res.send('Express is working on IISNode!');
});

expressServer.listen(process.env.PORT || 8080);

I don't even bother getting request handler at this point as the app = next() is failing.

Package.json:

"engines": {
    "node": "9.4.0 || 8.9.x"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",   
    "next": "^4.2.3",
    "next-redux-wrapper": "^1.3.5",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "nodemon": "^1.14.11"
  }

EDIT: I believe the issue is that next build needs to run first. I am looking if I can add some post deployment/build command with something like Kudu. If you have any suggestions please let me know.


Solution

  • You are right, you need to run next build first.

    So, this would work in Azure Web App if you create pages directory under project root and edit the package.json to add this:

    "scripts": {
        "postinstall": "next build"
    }