[Node.js, Express.js] Cannot find package 'express' imported from

I’m trying to deploy my Express.js app and it’s failing even though the build is successful…
It states that Cannot find package 'express' imported from /opt/render/project/src/src/index.js in the log, although I have Express installed. The directory of the index.js is also correct.
I have been tweaking a lot but am not able to deploy the app. Could someone help me out?

My package.json
{ "name": "take-root", "version": "1.0.0", "description": "", "type": "module", "prisma": { "seed": "node prisma/seed/seed.js" }, "main": "index.js", "scripts": { "start": "node src/index.js", "dev": "nodemon src/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "engines": { "node": "16.13.2" }, "author": "", "license": "ISC", "dependencies": { "@prisma/client": "^4.1.0", "bcrypt": "^5.0.1", "cors": "^2.8.5", "express": "^4.18.1", "jsonwebtoken": "^8.5.1", "morgan": "^1.10.0" }, "devDependencies": { "nodemon": "^2.0.19", "prisma": "^4.1.0" } }

Build command: node
Start command: npm run start

In index.js
`
import express from “express”;
import cors from “cors”;
import userRouter from “./routes/user.js”;
import raisedBedRouter from “./routes/raisedBed.js”;
import squareRouter from “./routes/square.js”;
import plantRouter from “./routes/plant.js”;

const app = express();
app.disable(“x-powered-by”);
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(“assets”));
app.use(express.static(“plant-image”));
app.use(express.static(“default-user-image”));

app.use(“/assets”, express.static(“assets”));
app.use(“/user”, userRouter);
app.use(“/raisedbed”, raisedBedRouter);
app.use(“/square”, squareRouter);
app.use(“/plant”, plantRouter);

const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(\n Server is running on http://localhost:${port}\n);`

Same problem here. Have you found solution?

If you’re seeing a similar issue, the resolution would be the same, you need to ensure dependencies are installed as part of the Build Command (set on settings page).

For example, a typical Node Web Service would usually need a Build Command set to:

npm install; npm build;

If you use npm, or:

yarn; yarn build;

If you use yarn.

Hope that helps

Alan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.