问题
I have a node.js app which uses express and runs locally with no problems. However, on Azure I am seeing:
Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\home\site\wwwroot\server.js:1:79)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
The packages.json file contains the dependency:
"express": "^4.15.3"
Assuming something had gone wrong with the npm install
, I went to the Kudu remote execution console and ran npm outdated
. No packages are missing.
This is my server.js file:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.port || 8000;
app.use(bodyParser.urlencoded({ extended: true }));
require('./app/routes')(app);
app.listen(port, () => {
});
I'm assuming it is something very obvious, but I can't work out what I'm missing.
回答1:
There isn't much we can really help with here, as the error you have provided indicates that the express
npm package has not been installed properly. So, I would suggest that you use App Service Editor (https://[YouAppName].scm.azurewebsites.net/dev/wwwroot/
) to troubleshoot this issue by checking whether the express
folder exists in the node_module
.
You can also run command npm install
in the console, restart your app and run it (Ctrl + F5
) in the browser.
回答2:
Assuming your Azure App Service is Windows environment, try to include node_modules
in the wwwroot
.
来源:https://stackoverflow.com/questions/44576294/error-cannot-find-module-express-when-running-on-azure