I am new in google cloud app engine. I am trying to deploy a node app to GCP. But the following error is showing:
Error: Cannot find module 'dotenv'
2021-02-14 06:07:04 backend[20210214t120450] Require stack:
2021-02-14 06:07:04 backend[20210214t120450] - /workspace/app.js
2021-02-14 06:07:04 backend[20210214t120450] at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
2021-02-14 06:07:04 backend[20210214t120450] at Function.Module._load (internal/modules/cjs/loader.js:667:27)
2021-02-14 06:07:04 backend[20210214t120450] at Module.require (internal/modules/cjs/loader.js:887:19)
2021-02-14 06:07:04 backend[20210214t120450] at require (internal/modules/cjs/helpers.js:74:18)
2021-02-14 06:07:04 backend[20210214t120450] at Object.<anonymous> (/workspace/app.js:5:1)
2021-02-14 06:07:04 backend[20210214t120450] at Module._compile (internal/modules/cjs/loader.js:999:30)
2021-02-14 06:07:04 backend[20210214t120450] at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
2021-02-14 06:07:04 backend[20210214t120450] at Module.load (internal/modules/cjs/loader.js:863:32)
2021-02-14 06:07:04 backend[20210214t120450] at Function.Module._load (internal/modules/cjs/loader.js:708:14)
2021-02-14 06:07:04 backend[20210214t120450] at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
2021-02-14 06:07:04 backend[20210214t120450] code: 'MODULE_NOT_FOUND',
2021-02-14 06:07:04 backend[20210214t120450] requireStack: [ '/workspace/app.js' ]
2021-02-14 06:07:04 backend[20210214t120450] }
My app.js:
const express = require("express");
const cors = require("cors");
const fileUpload = require("express-fileupload");
const MongoClient = require("mongodb").MongoClient;
const env = require("./config/config");
const uri = `${env.MONGO_CONN_URL}`;
const PORT = env.PORT;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(fileUpload());
app.get("/", (req, res) => {
res.send("Hello from db, it's working");
});
// connect to db
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect((err) => {
const applicantsCollection = client.db(`${env.DB_NAME}`).collection("applicants");
// Upload Endpoint
app.post("/upload", (req, res) => {
const file = req.files.file;
const name = req.body.name;
const email = req.body.email;
const work = req.body.work;
if (req.files === null) {
return res.status(400).json({ msg: "No file uploaded" });
}
applicantsCollection.insertOne({ name, email, work, file }).then((result) => {
res.send(result.insertedCount > 0);
console.log(result);
});
});
});
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
});
My config.js:
require("dotenv").config();
const env = {
PORT: process.env.PORT,
MONGO_CONN_URL: process.env.MONGO_CONN_URL,
DB_NAME: process.env.DB_NAME,
};
module.exports = env;
package.json:
{
"name": "audify-tech-backend",
"version": "1.0.0",
"main": "app.js",
"license": "MIT",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
},
"keywords": [
"node",
"javascript"
],
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express-fileupload": "^1.2.1",
"mongodb": "^3.6.3",
"nodemon": "^2.0.7"
},
"devDependencies": {
"dotenv": "^8.2.0"
}
}
I have also tried out this way by changing dev property in package.json:
{
"name": "audify-tech-backend",
"version": "1.0.0",
"main": "app.js",
"license": "MIT",
"scripts": {
"start": "node app.js",
"dev": "nodemon -r dotenv/config app.js"
},
"keywords": [
"node",
"javascript"
],
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express-fileupload": "^1.2.1",
"mongodb": "^3.6.3",
"nodemon": "^2.0.7"
},
"devDependencies": {
"dotenv": "^8.2.0"
}
}
app.yaml:
runtime: nodejs12
instance_class: B1
service: backend
basic_scaling:
max_instances: 1
env_variables:
PORT: 8080
MONGO_CONN_URL: mongodb connection string
DB_NAME: "db"
GOOGLE_CLOUD_PROJECT_ID: "abcd-website"
But the error remains same in google cloud that can't find module "dotenv". I have also tried by reinstalling dotenv. But it doesn't work. All the possible ways I have tried is working well in local machine.
Move dotenv
from devDependencies to dependencies
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.