简体   繁体   English

找不到模块文件

[英]cannot find module file

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"我正在做我的 Udemy 课程,现在我是堆栈;(如果有人可以帮助我,那就太好了:所以错误说“错误。找不到模块'/Users/yui/Desktop/todolist-v1/date:js' 需要堆栈。-/Users/yui/Desktop/todolist-v1/app.js"

but I am not sure why they cannot find module.但我不确定他们为什么找不到模块。 Thanks in advance.提前致谢。

app.js file app.js 文件

//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日期.js 文件

//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.如果 app.js 和 date.js 在同一个文件夹中,则不需要使用 __dirname。

Just use this line of code for import any js file.只需使用这行代码导入任何 js 文件。

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您可以使用这行代码从 app.js 导入 helpers 文件夹中的任何 js 文件

const getDate = require('./helpers/date.js');

There is no use of using _dirname in const date = require(__dirname + "/date.js");const date = require(__dirname + "/date.js");中使用_dirname是没有用的Here is the explanation, why?这是解释,为什么?
In Node.js, __dirname is always the directory in which the currently executing script resides.__dirname中,__dirname 始终是当前执行脚本所在的目录。 So if you typed __dirname into /dir1/dir2/myscript.js , the value would be /d1/d2 .因此,如果您将__dirname键入/dir1/dir2/myscript.js ,则该值将是/d1/d2

By contrast, .相比之下, . gives you the directory from which you ran the node command in your terminal window (ie your working directory) when you use libraries like path and fs .当您使用pathfs等库时,为您提供在终端 window 中运行node命令的目录(即您的工作目录)。 Technically, it starts out as your working directory but can be changed using process.chdir() .从技术上讲,它从您的工作目录开始,但可以使用process.chdir()进行更改。

The exception is when you use .例外是当您使用. with require() .require() The path inside require is always relative to the file containing the call to require . require中的路径总是相对于包含对require的调用的文件。

For example...例如...

Let's say your directory structure is假设您的目录结构是

|--dir1
  |--dir2
     pathtest.js

and pathtest.js containspathtest.js包含

var path = require("path");
console.log(". = %s", path.resolve("."));
console.log("__dirname = %s", path.resolve(__dirname));

and you do你也是

cd /dir1/dir2
node pathtest.js

you get你得到

. = /dir1/dir2
__dirname = /dir1/dir2

Your working directory is /dir1/dir2 so that's what .你的工作目录是/dir1/dir2所以这就是. resolves to.解决。 Since pathtest.js is located in /dir1/dir2 that's what __dirname resolves to as well.由于pathtest.js位于/dir1/dir2中,这__dirname解析的内容。

However, if you run the script from /dir1但是,如果您从/dir1运行脚本

cd /dir1
node dir2/pathtest.js

you get你得到

. = /dir1
__dirname = /dir1/dir2

In that case, your working directory was /dir1 so that's what .在这种情况下,您的工作目录是/dir1所以这就是. resolved to, but __dirname still resolves to /dir1/dir2 .解析为,但__dirname仍解析为/dir1/dir2

Using .使用. inside require ...里面require ...

If inside dir2/pathtest.js you have a require call into include a file inside dir1 you would always do如果在dir2/pathtest.js ,您require调用在dir1中包含一个文件,您总是会这样做

require('../thefile')

because the path inside require is always relative to the file in which you are calling it.因为require中的路径总是相对于你调用它的文件。 It has nothing to do with your working directory.它与您的工作目录无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM