I am doing my Udemy course and I am stack now ;( If anyone can help me it would be great! So the Error said "Error: Cannot find module '/Users/yui/Desktop/todolist-v1/date.js' Require stack: - /Users/yui/Desktop/todolist-v1/app.js"
but I am not sure why they cannot find module. Thanks in advance.
app.js file
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const app = express();
let items = ["Buy Food","Cook Food","Eat Food"];
let workItems = [];
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res){
let day = date();
res.render("list", {ListTitle: day, NewListItems: items});
});
app.post("/", function(req, res){
let item = req.body.newItem;
if(req.body.list === "Work"){
workItems.push(item);
res.redirect("/work");
}else{
items.push(item);
res.redirect("/")
}
});
app.get("/about", function(req, res){
res.render("about")
});
app.get("/work", function(req, res){
res.render("list", {ListTitle: "Work List", NewListItems: workItems});
});
app.post("/work", function(req, res){
let item = req.body.newItem;
workItems.push(item);
res.redirect("/work");
})
app.listen(3000, function(){
console.log("Server is running on port 3000");
});
date.js file
//jshint esversion:6
module.exports = getDate;
function getDate(){
let today = new Date();
let currentDay = today.getDay();
let day = "";
let options = {
weekday: "long",
day: "numeric",
month: "long"
};
let day = today.toLocaleDateString("en-US", options);
};
If app.js and date.js in the same folder you didn't need to use __dirname.
Just use this line of code for import any js file.
const getDate = require('./date.js');
For example if this your project structure
/main_app_folder/app.js
/main_app_folder/helpers/data.js
You can use this line of code for import any js file inside helpers folder from app.js
const getDate = require('./helpers/date.js');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments