589 内置模块path

1.1. 认识path模块

path模块用于对路径和文件进行处理,提供了很多好用的方法。

并且我们知道在Mac OS、Linux和window上的路径是不一样的

  • window上会使用 或者 \ 来作为文件路径的分隔符,当然目前也支持 /
  • 在Mac OS、Linux的Unix操作系统上使用 / 来作为文件路径的分隔符;

那么如果我们在window上使用 来作为分隔符开发了一个应用程序,要部署到Linux上面应该怎么办呢?

  • 显示路径会出现一些问题;
  • 所以为了屏蔽他们之间的差异,在开发中对于路径的操作我们可以使用 path 模块;

1.2. path常见的API

从路径中获取信息

  • dirname:获取文件的父文件夹;
  • basename:获取文件名;
  • extname:获取文件扩展名;
const path = require("path");

const myPath = '/Users/hahaha/Desktop/Node/课堂/PPT/01_邂逅Node.pdf';

const dirname = path.dirname(myPath);
const basename = path.basename(myPath);
const extname = path.extname(myPath);

console.log(dirname); // /Users/hahaha/Desktop/Node/课堂/PPT
console.log(basename); // 01_邂逅Node.pdf
console.log(extname); // .pdf

路径的拼接

  • 如果我们希望将多个路径进行拼接,但是不同的操作系统可能使用的是不同的分隔符;
  • 这个时候我们可以使用path.join函数;
console.log(path.join('/user', 'why', 'abc.txt'));

将文件和某个文件夹拼接

  • 如果我们希望将某个文件和文件夹拼接,可以使用 path.resolve;
    • resolve函数会判断我们拼接的路径前面是否有 /.././
    • 如果有表示是一个绝对路径,会返回对应的拼接路径;
    • 如果没有,那么会和当前执行文件所在的文件夹进行路径的拼接
path.resolve('abc.txt'); // /Users/hahaha/Desktop/Node/TestCode/04_learn_node/06_常见的内置模块/02_文件路径/abc.txt
path.resolve('/abc.txt'); // /abc.txt
path.resolve('/User/why', 'abc.txt'); // /User/why/abc.txt
path.resolve('User/why', 'abc.txt'); // /Users/hahaha/Desktop/Node/TestCode/04_learn_node/06_常见的内置模块/02_文件路径/User/why/abc.txt

resolve其实我们在webpack中也会使用:

const CracoLessPlugin = require('craco-less');
const path = require("path");

const resolve = dir => path.resolve(__dirname, dir);

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: { '@primary-color': '#1DA57A' },
            javascriptEnabled: true,
          },
        },
      },
    }
  ],
  webpack: {
    alias: {
      "@": resolve("src"),
      "components": resolve("src/components")
    }
  }
}

01_路径的演练.js

const path = require('path');

const basePath = '/User/why';
const filename = 'abc.txt';

// const path = basePath + "/" + filename;

const filepath = path.resolve(basePath, filename);
console.log(filepath);

02_path其他方法.js

const path = require('path');

// 1.获取路径的信息
// const filepath = '/User/why/abc.txt';
// console.log(path.dirname(filepath)); // /User/why
// console.log(path.basename(filepath)); // abc.txt
// console.log(path.extname(filepath)); // .txt


// 2.join路径拼接
const basepath = '../User/why';
const filename = './abc.txt';
const othername = './why.js';

const filepath1 = path.join(basepath, filename);
// console.log(filepath1); // ..Userwhyabc.txt

// 3.resolve路径拼接
// resolve会判断拼接的路径字符串中,是否有以/或./或../开头的路径
// const filepath2 = path.resolve(basepath, filename, othername);
// console.log(filepath2); // F:前端why
odekejianday01_24Userwhyabc.txtwhy.js

const basepath2 = '/User/hahaha';
// const filename2 = '/why/abc.txt'; // /why/abc.txt
// const filename2 = './why/abc.txt'; // /User/hahaha/why/abc.txt
// const filename2 = 'why/abc.txt'; // /User/hahaha/why/abc.txt

const filename2 = '../why/abc.txt'; // /User/hahaha/why/abc.txt

const result = path.resolve(basepath2, filename2);
console.log(result);

03.使用es module加载.mjs

import path from 'path';

const basepath = '../User/why';
const filename = '/abc.txt';
const othername = '/why.js';

const filepath1 = path.join(basepath, filename);
console.log(filepath1);
原文地址:https://www.cnblogs.com/jianjie/p/14343052.html