xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js path All In One

Node.js path All In One

const path = require('path');
const app = '/users/github/xgqfrms/app.ts';

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

const log = console.log;

log('__dirname =', __dirname);

log('basename =', basename);
log('extname =', extname);
log('dirname =', dirname);

/*

__dirname = /Users/xgqfrms-mbp/Documents/GitHub/demo/src
dirname = /users/github/xgqfrms
basename = app.ts
extname = .ts

*/

$ node ./src/app.js

https://nodejs.dev/learn/nodejs-file-paths

path.parse() & path.format()

root: the root
dir: the folder path starting from the root
base: the file name + extension
name: the file name
ext: the file extension


const path = require('path');

path.parse('/users/test.txt');

/*
{
  root: '/',
  dir: '/users',
  base: 'test.txt',
  ext: '.txt',
  name: 'test'
}
*/

root: the root
dir: the folder path starting from the root
base: the file name + extension
name: the file name
ext: the file extension

const path = require('path');

const appRoot = '/users/xgqfrms/';
const appDir = '/users/xgqfrms/github';
const appBase = 'app.ts';
const appName = 'app';
const appExtension = '.ts';

// POSIX
const filename = path.format({
  dir: appDir,
  base: appBase,
});

const filename2 = path.format({
  root: appRoot,
  name: appName,
  ext: appExtension,
});

// Windows 磁盘分区 C:
const filename3 = path.format({
  dir: 'C:\\Users\\xgqfrms\\github',
  base: 'app.ts',
});

const log = console.log;

// log('__dirname =', __dirname);

log('filename =', filename);
log('filename2 =', filename2);
log('filename3 =', filename3);

/*

filename = /users/xgqfrms/github/app.ts
filename2 = /users/xgqfrms/app.ts
filename3 = C:\Users\xgqfrms\github/app.ts

*/


$ node ./src/fromat.js

https://nodejs.dev/learn/the-nodejs-path-module

ES5

// commonjs
const path = require('path')


ES6

// esm
import path form 'path';


demo

CJS

var fs = require("fs");
var path = require("path");

const log = console.log;
console.log('commonjs __dirname =', __dirname);
// commonjs __dirname = /Users/xgqfrms-mbp/Documents/GitHub/app/src

// / 当前文件路径下创建文件夹 ✅  (./src => ./src/upload)
// const dir = __dirname + '/upload';
// console.log('commonjs dir =', dir);
// commonjs dir = /Users/xgqfrms-mbp/Documents/GitHub/app/src/upload

// ../ 当前文件上一层路径下创建文件夹 ❌ (./src => ./upload)
// const dir = __dirname + '../upload';
// console.log('commonjs dir =', dir);
// Error: ENOENT: no such file or directory, mkdir '/Users/xgqfrms-mbp/Documents/GitHub/app/src../upload'

// ../ 当前文件上一层路径下创建文件夹 ✅  (./src => ./upload)
// const dir = path.resolve('../upload');
const dir = path.resolve(__dirname, '../upload');
console.log('commonjs dir =', dir);
// commonjs dir = /Users/xgqfrms-mbp/Documents/GitHub/app/upload


if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     recursive: true,
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


/*

// mjs
node --experimental-modules ./src/mkdir.mjs

# "type": "module",
node ./src/mkdir.js

node ./src/mkdir.cjs

*/

ESM

import fs from "fs";
import path from "path";


const log = console.log;

// 指定当前 root 路径 ✅
const __dirname = path.resolve('./src');

console.log('esm __dirname =', __dirname);
// esm __dirname = /Users/xgqfrms-mbp/Documents/GitHub/app

// ../ 当前文件上一层路径下创建文件夹 ✅  (./src => ./upload)
const dir = path.resolve(__dirname, '../upload');
log('esm dir =', dir);
// esm dir = /Users/xgqfrms-mbp/Documents/GitHub/app/upload

// ../ 当前文件上一层路径下创建文件夹 ❌  (./src => ./upload)
// const dir = __dirname + '../upload';
// log('esm dir =', dir);
// Error: ENOENT: no such file or directory, mkdir '/Users/xgqfrms-mbp/Documents/GitHub/app/src../upload'

if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }

// if (!fs.existsSync(dir)) {
//   fs.mkdirSync(dir, {
//     recursive: true,
//     mode: 0o744,
//   });
//   // mode's default value is 0o744
// }


/*

node --experimental-modules ./src/mkdir-src.mjs

node ./src/mkdir-src.js

*/

refs

https://nodejs.org/api/path.html

https://stackoverflow.com/questions/21194934/how-to-create-a-directory-if-it-doesnt-exist-using-node-js/71735771#71735771



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-17 14:08  xgqfrms  阅读(31)  评论(1编辑  收藏  举报