博客
关于我
node全局变量 __dirname __filename 获取绝对路径
阅读量:264 次
发布时间:2019-03-01

本文共 415 字,大约阅读时间需要 1 分钟。

__dirname 和 __filename 获取绝对路径

在 Node.js 中,获取文件或文件夹的绝对路径可以通过两个全局变量轻松实现:__dirname 和 __filename。

这些变量的含义如下:

  • __dirname:表示当前文件所在目录的绝对路径。
  • __filename:表示当前文件的绝对路径。

这些全局变量具有以下特点:

  • 变量特性:在不同文件中值不同,随文件位置变化而变化。
  • 全局特性:可以在任何位置直接使用,无需声明。

测试使用方法很简单,随时随地都可以执行:

console.log(__dirname); console.log(__filename);

在文件操作中,建议使用绝对路径,可以通过以下方式构建:

const fs = require('fs');const filePath = __dirname + '\\html5.jpg';

即使是在复杂文件系统中,这种方法依然能有效获取文件路径。

转载地址:http://bdca.baihongyu.com/

你可能感兴趣的文章
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
os.environ 没有设置环境变量
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.system 在 Python 中不起作用
查看>>
OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
查看>>
OSCACHE介绍
查看>>
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
查看>>
OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
查看>>
SQL--mysql索引
查看>>
OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
查看>>