04-Node.js学习笔记-相对路径VS绝对路径

4.1相对路径VS绝对路径

  • 大多数情况下使用绝对路径,因为相对路径有时候相对的是命令行工具的当前工作目录
  • 在读取文件或者设置文件路径时都会选择绝对路径

4.2使用__dirname 获取当前文件所在的绝对路径

    const fs = require('fs');
    const path = require('path');
    console.log(__dirname);
    console.log(path.join(__dirname,'01.hello.js'))
    fs.readFile(path.join(_dirname,'01.hello.js'),'utf8',(err,doc)=>{
        console.log(err)
        console.log(doc)
    }

猜你喜欢

转载自www.cnblogs.com/foreverLuckyStar/p/12057948.html