node 中路径的问题

node中用相对路径会根据启动脚本的位置来决定。例如

draw 

  server

    server.js

要是是用node server/server.js来启动脚本的话,采用path.resolve来将./变成绝对路径,会发现 /user/xx/draw

如果是用在server底下用node server.js来启动的话,则路径为/user/xx/draw/server

用__dirname __filename都是绝对路径。

而用process.cwd()也是绝对路径,但是还是会根据启动的位置的不同而不同。和path.resolve('./')的结果相同。

原文地址:https://www.cnblogs.com/zhuni/p/6385569.html