Home > Net >  create react app with express on azure app service fails to start
create react app with express on azure app service fails to start

Time:02-25

I have deployed to azure a create-react-app that has a server.js file for dynamically generating meta tags.

The stack is Node 16 on Linux

In my startup command I have pm2 start /home/site/wwwroot/server.js --no-daemon

My server.js is as follows

const express = require("express");
const path = require("path");
const fs = require("fs");

const PORT = process.env.PORT || 5000;

const app = express();

app.get("/", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "Home Page").replace(/__META_DESCRIPTION__/g, "Home page description.");

    res.send(data);
  });
});

app.get("/snp/list", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "SNP List").replace(/__META_DESCRIPTION__/g, "SNP List description.");

    res.send(data);
  });
});

app.get("/snp/detail", (req, res) => {
  const filePath = path.resolve(__dirname, "index.html");
  fs.readFile(filePath, "utf8", (err, data) => {
    if (err) {
      return console.log(err);
    }

    data = data.replace(/__META_OG_TITLE__/g, "SNP Detail").replace(/__META_DESCRIPTION__/g, "SNP detail description.");

    res.send(data);
  });
});

app.use(express.static(path.resolve(__dirname)));

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

This server.js is a sibling of the index.html.

I'm getting a Cannot find module 'express' error and this is my error log dump

 2022-02-23T22:12:16.154879649Z NodeJS Version : v16.6.1
2022-02-23T22:12:16.154883449Z Note: Any data outside '/home' is not persisted
2022-02-23T22:12:16.154887149Z 
2022-02-23T22:12:16.491995689Z Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2022-02-23T22:12:16.492031589Z Could not find operation ID in manifest. Generating an operation id...
2022-02-23T22:12:16.492036389Z Build Operation ID: 8e9bf4f8-2b72-4d38-ad94-8e3461d1caed
2022-02-23T22:12:17.563270289Z Environment Variables for Application Insight's IPA Codeless Configuration exists..
2022-02-23T22:12:17.626540290Z Writing output script to '/opt/startup/startup.sh'
2022-02-23T22:12:17.864719008Z Running #!/bin/sh
2022-02-23T22:12:17.865929003Z 
2022-02-23T22:12:17.865970803Z # Enter the source directory to make sure the script runs where the user expects
2022-02-23T22:12:17.866221302Z cd "/home/site/wwwroot"
2022-02-23T22:12:17.866787300Z 
2022-02-23T22:12:17.866803800Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2022-02-23T22:12:17.866809300Z if [ -z "$PORT" ]; then
2022-02-23T22:12:17.867117999Z      export PORT=8080
2022-02-23T22:12:17.867133499Z fi
2022-02-23T22:12:17.867138499Z 
2022-02-23T22:12:17.868571094Z PATH="$PATH:/home/site/wwwroot" pm2 start /home/site/wwwroot/server.js --no-daemon
2022-02-23T22:12:18.222455804Z 
2022-02-23T22:12:18.222498404Z                         -------------
2022-02-23T22:12:18.222506204Z 
2022-02-23T22:12:18.222510804Z __/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
2022-02-23T22:12:18.222516104Z  _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
2022-02-23T22:12:18.222521604Z   _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
2022-02-23T22:12:18.222526904Z    _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
2022-02-23T22:12:18.222533104Z     _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
2022-02-23T22:12:18.222550803Z      _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
2022-02-23T22:12:18.222555903Z       _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
2022-02-23T22:12:18.222560103Z        _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
2022-02-23T22:12:18.222564603Z         _\///______________\///______________\///__\///////////////__
2022-02-23T22:12:18.222568703Z 
2022-02-23T22:12:18.222572503Z 
2022-02-23T22:12:18.222576203Z                           Runtime Edition
2022-02-23T22:12:18.222580003Z 
2022-02-23T22:12:18.222583703Z         PM2 is a Production Process Manager for Node.js applications
2022-02-23T22:12:18.222587503Z                      with a built-in Load Balancer.
2022-02-23T22:12:18.222591303Z 
2022-02-23T22:12:18.222595103Z                 Start and Daemonize any application:
2022-02-23T22:12:18.222598903Z                 $ pm2 start app.js
2022-02-23T22:12:18.222602603Z 
2022-02-23T22:12:18.222606303Z                 Load Balance 4 instances of api.js:
2022-02-23T22:12:18.222610003Z                 $ pm2 start api.js -i 4
2022-02-23T22:12:18.222613803Z 
2022-02-23T22:12:18.222619703Z                 Monitor in production:
2022-02-23T22:12:18.222623703Z                 $ pm2 monitor
2022-02-23T22:12:18.222627503Z 
2022-02-23T22:12:18.222631203Z                 Make pm2 auto-boot at server restart:
2022-02-23T22:12:18.222635003Z                 $ pm2 startup
2022-02-23T22:12:18.222638703Z 
2022-02-23T22:12:18.222642403Z                 To go further checkout:
2022-02-23T22:12:18.222646103Z                 http://pm2.io/
2022-02-23T22:12:18.222649803Z 
2022-02-23T22:12:18.222653603Z 
2022-02-23T22:12:18.222657303Z                         -------------
2022-02-23T22:12:18.222661103Z 
2022-02-23T22:12:18.231376071Z pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
2022-02-23T22:12:18.447267984Z 2022-02-23T22:12:18: PM2 log: Launching in no daemon mode
2022-02-23T22:12:18.481629859Z 2022-02-23T22:12:18: PM2 log: [PM2] Starting /home/site/wwwroot/server.js in fork_mode (1 instance)
2022-02-23T22:12:18.486247242Z 2022-02-23T22:12:18: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:18.515573535Z 2022-02-23T22:12:18: PM2 log: App [server:0] online
2022-02-23T22:12:18.529572684Z 2022-02-23T22:12:18: PM2 log: [PM2] Done.
2022-02-23T22:12:18.632207310Z 2022-02-23T22:12:18: PM2 log: ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
2022-02-23T22:12:18.632269910Z │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
2022-02-23T22:12:18.632279110Z ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
2022-02-23T22:12:18.632284510Z │ 0   │ server    │ default     │ N/A     │ fork    │ 39       │ 0s     │ 0    │ online    │ 0%       │ 30.6mb   │ root     │ disabled │
2022-02-23T22:12:18.632289010Z └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2022-02-23T22:12:18.638526987Z 2022-02-23T22:12:18: PM2 log: [--no-daemon] Continue to stream logs
2022-02-23T22:12:18.639576183Z 2022-02-23T22:12:18: PM2 log: [--no-daemon] Exit on target PM2 exit pid=28
2022-02-23T22:12:18.763260232Z 2022-02-23T22:12:18: PM2 log: [PM2] This PM2 is not UP TO DATE
2022-02-23T22:12:18.766529521Z 2022-02-23T22:12:18: PM2 log: [PM2] Upgrade to version 5.2.0
2022-02-23T22:12:18.769252311Z 22:12:18 PM2      | [PM2] This PM2 is not UP TO DATE
2022-02-23T22:12:18.770937304Z 22:12:18 PM2      | [PM2] Upgrade to version 5.2.0
2022-02-23T22:12:18.789006939Z 22:12:18 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:18.790186234Z 22:12:18 0|server | Require stack:
2022-02-23T22:12:18.791370430Z 22:12:18 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:18.793005124Z 22:12:18 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:18.794990917Z 22:12:18 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:18.799204201Z 22:12:18 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:18.800213398Z 22:12:18 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:18.801263394Z 22:12:18 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:18.802277190Z 22:12:18 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:18.803243187Z 22:12:18 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:18.804699981Z 22:12:18 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:18.805678378Z 22:12:18 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:18.806396875Z 22:12:18 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:18.807571271Z 22:12:18 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:18.807588071Z 22:12:18 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:18.810564360Z 22:12:18 0|server | }
2022-02-23T22:12:18.810584860Z 2022-02-23T22:12:18: PM2 log: App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:18.814429746Z 22:12:18 PM2      | App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:18.816066740Z 2022-02-23T22:12:18: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:18.817591534Z 22:12:18 PM2      | App [server:0] starting in -fork mode-
2022-02-23T22:12:18.837398662Z 2022-02-23T22:12:18: PM2 log: App [server:0] online
2022-02-23T22:12:18.839963853Z 22:12:18 PM2      | App [server:0] online
2022-02-23T22:12:19.077033189Z 22:12:19 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:19.078228284Z 22:12:19 0|server | Require stack:
2022-02-23T22:12:19.079877278Z 22:12:19 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:19.081284973Z 22:12:19 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:19.081781471Z 22:12:19 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:19.082940467Z 22:12:19 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:19.084115863Z 22:12:19 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:19.085170459Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:19.086312955Z 22:12:19 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:19.089914542Z 22:12:19 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:19.090947538Z 22:12:19 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:19.091896934Z 22:12:19 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:19.093072430Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:19.096341718Z 22:12:19 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:19.097369815Z 22:12:19 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:19.099276608Z 22:12:19 0|server | }
2022-02-23T22:12:19.100614503Z 2022-02-23T22:12:19: PM2 log: App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:19.104935187Z 22:12:19 PM2      | App [server:0] exited with code [1] via signal [SIGINT]
2022-02-23T22:12:19.106340982Z 2022-02-23T22:12:19: PM2 log: App [server:0] starting in -fork mode-
2022-02-23T22:12:19.107983276Z 22:12:19 PM2      | App [server:0] starting in -fork mode-
2022-02-23T22:12:19.122067624Z 2022-02-23T22:12:19: PM2 log: App [server:0] online
2022-02-23T22:12:19.124422816Z 22:12:19 PM2      | App [server:0] online
2022-02-23T22:12:19.342399121Z 22:12:19 0|server | Error: Cannot find module 'express'
2022-02-23T22:12:19.344263414Z 22:12:19 0|server | Require stack:
2022-02-23T22:12:19.346447807Z 22:12:19 0|server | - /home/site/wwwroot/server.js
2022-02-23T22:12:19.347436203Z 22:12:19 0|server |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-02-23T22:12:19.351468388Z 22:12:19 0|server |     at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
2022-02-23T22:12:19.352447085Z 22:12:19 0|server |     at require (node:internal/modules/cjs/helpers:94:18)
2022-02-23T22:12:19.353268082Z 22:12:19 0|server |     at Object.<anonymous> (/home/site/wwwroot/server.js:1:17)
2022-02-23T22:12:19.354038979Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-02-23T22:12:19.354565177Z 22:12:19 0|server |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-02-23T22:12:19.355181375Z 22:12:19 0|server |     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-02-23T22:12:19.356654469Z 22:12:19 0|server |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-02-23T22:12:19.357570966Z 22:12:19 0|server |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
2022-02-23T22:12:19.360012257Z 22:12:19 0|server |     at Module._compile (node:internal/modules/cjs/loader:1101:14) {
2022-02-23T22:12:19.360040657Z 22:12:19 0|server |   code: 'MODULE_NOT_FOUND',
2022-02-23T22:12:19.360920954Z 22:12:19 0|server |   requireStack: [ '/home/site/wwwroot/server.js' ]
2022-02-23T22:12:19.362052350Z 22:12:19 0|server | }

If I switch the startup command to pm2 serve /home/site/wwwroot --no-daemon --spa then every things works just fine.

And, locally from the build folder if I issue a node server.js then again it works perfectly at http://localhost:5000/

I've spent the whole day trying to figure out what might be wrong and any help will be much appreciated.

EDIT

For now I am simply ftp'ing all the files from my build folder to site/wwwroot. I'm not worried at this point about setting up a CI/CD chain since I just first want the app to run.

The startup command is being set in the azure portal under settings/configuration/general settings/startup Command

EDIT-2 I installed pm2 via npm install pm2 -g locally on my machine and from the build folder if I issue the command pm2 start server.js then the app launches successfully.

My local node version is v14.5.0

CodePudding user response:

  • When you create production build for your React, Angular or other Node framework the files will either be placed in a build or dist directory(folder), depending on the framework.
  • App Service on Linux uses Oryx to detect, build, and start your application.
  • Using --spa will automatically redirect all queries to the index.html_
pm2 serve /home/site/wwwroot --no-daemon --spa
  • In order to serve the built content, you can perform either of the following:

PM2 Serve

  1. If all of the items in the build directory are in wwwroot, change the path to /home/site/wwwroot.

    pm2 serve /home/site/wwwroot/build --no-daemon    
    
  2. If the content is under dist folder , make sure to use

    pm2 serve /home/site/wwwroot/dist --no-daemon
    

Process File

  • Create a process.json or process.yml , place it in /home/site/wwwroot.

       {
         "script": "serve",
         "env": {
             "PM2_SERVE_PATH": './build'
         }
         "args": '--no-daemon'
         }    
    
  • In the Azure Portal, go to Configuration.=> General and locate the Startup Command box and enter process.json =>Save.

Please refer Configure Node.js server , deploy React application in Azure App Service and Using PM2 on App Service Linux for more information

CodePudding user response:

The solution to the issue was staring at me in the face.

All I had to do was SSH to the home/site/wwwroot & npm install express.

Everything works perfectly with my meta tags getting dynamically generated.

  • Related