I am working on a mini project on CI/CD pipeline. I am working on a basic nodejs code and deploying it on EC2 server using pipeline. The code works fine locally but the pipeline fails in code deploy part. I have installed Node, Npm, Code deploy agent in ec2 and attached the required roles as well in both ec2 and code deploy. The error come in application_start phase
Nodejs-project
const express = require('express');
const app = express();
const { readFile } = require('fs').promises;
const path = require('path'); // Add this line
app.use(express.static(path.join(__dirname, 'public'))); // Serve static files
app.get('/', async (request, response) => {
response.send(await readFile('./public/home.html', 'utf8')); // Update the path
});
app.listen(process.env.PORT || 3000, () => console.log(`App available on http://localhost:3000`))
Appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 200
runas: root
ApplicationStart:
- location: scripts/application_start.sh
timeout: 200
runas: root
before_install.sh
# Install Node.js dependencies
npm ls
npm install express
application_start.sh
#node index.js
node .
ERROR
LifecycleEvent - ApplicationStart
Script - scripts/application_start.sh
[stderr]internal/modules/cjs/loader.js:818
[stderr] throw err;
[stderr] ^
[stderr]
[stderr]Error: Cannot find module '/opt/codedeploy-agent'
[stderr] at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
[stderr] at Function.Module._load (internal/modules/cjs/loader.js:667:27)
[stderr] at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
[stderr] at internal/main/run_main_module.js:17:47 {
[stderr] code: 'MODULE_NOT_FOUND',
[stderr] requireStack: []
[stderr]}