I am getting the error below while deploying my NodeJS app on Azure:
node:internal/modules/cjs/loader
Error: Cannot find module '...'
According to some research, it should be a node version issue.
My App is supposed to run with the stack Node-18-lts
, and I set the engines accordingly in my packages.json
file, but when I bash into Kudu, I see node 14.
Why isn't it Node 18 ? I don't know if that will fix my issue, but at least, it seems to be an issue too.
Log Stream extract:
| npm info using npm@9.6.4
| npm info using node@v18.19.1
| > my-api@0.1.0 start
| > strapi start
| node:internal/modules/cjs/loader:1137
| throw err;
| ^
| Error: Cannot find module '../dist/cli'
| Require stack:
| - /home/site/wwwroot/node_modules/.bin/strapi
| at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)
| at Module._load (node:internal/modules/cjs/loader:975:27)
| at Module.require (node:internal/modules/cjs/loader:1225:19)
| at require (node:internal/modules/helpers:177:18)
| at Object.<anonymous> (/home/site/wwwroot/node_modules/.bin/strapi:2:1)
| at Module._compile (node:internal/modules/cjs/loader:1356:14)
| at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
| at Module.load (node:internal/modules/cjs/loader:1197:32)
| at Module._load (node:internal/modules/cjs/loader:1013:12)
| at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) {
| code: 'MODULE_NOT_FOUND',
| requireStack: [ '/home/site/wwwroot/node_modules/.bin/strapi' ]
| }
Kudu Console output:
__ __ __ __ _ __
/ //_/_ _ ____/ /_ __/ / (_) /____
/ ,< / / / / __ / / / / / / / __/ _
/ /| / /_/ / /_/ / /_/ / /___/ / /_/ __/
/_/ |_\__,_/\__,_/\__,_/_____/_/\__/\___/
DEBUG CONSOLE | AZURE APP SERVICE ON LINUX
Documentation: http://aka.ms/webapp-linux
Kudu Version : 20240315.1
Commit : xxx
kudu_ssh_user@xxx:/$ node -v
v14.19.2
The error you're encountering is not related to node versions.
In the Bash section of Azure Web App, the default NodeJS version is v14.19.2
When I checked the NodeJs version via SSH console it correctly gave Node 18.19.1 version.
According to the error message you've provided, I added the line below to your package.json file, as described in this Document
"scripts": {
"start": "node node_modules/@strapi/strapi/bin/strapi.js start"
}
Below is the complete package.json file:
Package.json:
{
"name": "osanwebsite-api",
"private": true,
"version": "0.1.0",
"description": "A Strapi application",
"scripts": {
"develop": "strapi develop",
"start": "node node_modules/@strapi/strapi/bin/strapi.js start",
"build": "strapi build",
"strapi": "strapi"
},
"devDependencies": {},
"dependencies": {
"@strapi/plugin-cloud": "4.23.1",
"@strapi/plugin-graphql": "4.23.1",
"@strapi/plugin-i18n": "4.23.1",
"@strapi/plugin-users-permissions": "4.23.1",
"@strapi/provider-upload-aws-s3": "^4.24.0",
"@strapi/strapi": "4.23.1",
"better-sqlite3": "8.6.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "5.3.4",
"styled-components": "5.3.3"
},
"author": {
"name": "A Strapi developer"
},
"strapi": {
"uuid": "53252aee-466b-4b03-92c9-72ece5437d0c"
},
"engines": {
"node": "18.16.1",
"npm": "9.5.1
},
"license": "MIT"
}
After deploying the app to Azure App Service, I added the following startup command in app configurations.
pm2 serve /home/site/wwwroot/build --no-daemon --spa
Here's the output after deployment: