我在 Azure 上部署 NodeJS 应用程序时收到以下错误:
node:internal/modules/cjs/loader
Error: Cannot find module '...'
根据一些研究,应该是node版本问题。 我的应用程序应该与堆栈一起运行
Node-18-lts
,并且我在 packages.json
文件中相应地设置了引擎,但是当我猛击到 Kudu 时,我看到节点 14。
为什么不是 Node 18 ?我不知道这是否能解决我的问题,但至少,这似乎也是一个问题。
日志流提取:
| npm info using [email protected]
| npm info using [email protected]
| > [email protected] 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 控制台输出:
__ __ __ __ _ __
/ //_/_ _ ____/ /_ __/ / (_) /____
/ ,< / / / / __ / / / / / / / __/ _
/ /| / /_/ / /_/ / /_/ / /___/ / /_/ __/
/_/ |_\__,_/\__,_/\__,_/_____/_/\__/\___/
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
您遇到的错误与节点版本无关。
在 Azure Web App 的 Bash 部分,默认 NodeJS 版本是 v14.19.2
当我通过 SSH 控制台检查 NodeJs 版本时,它正确地给出了 Node 18.19.1 版本。
根据您提供的错误消息,我将以下行添加到您的 package.json 文件中,如本 Document
中所述"scripts": {
"start": "node node_modules/@strapi/strapi/bin/strapi.js start"
}
下面是完整的package.json文件:
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"
}
将应用程序部署到Azure App Service后,我在应用程序配置中添加了以下启动命令。
pm2 serve /home/site/wwwroot/build --no-daemon --spa
这是部署后的输出: