Node.js path All In One
1.Adafruit & CircuitPython & Node.js All In One2.Node.js os module All In One3.Node.js & Raspberry Pi & WS2812B RGB LEDs strip All In One4.Node.js Buffer.from All In One5.Node.js Event Loop & V8 engine & libuv All In One6.Node.js & npm package.json exports field All In One7.Node.js cli tools auto install npm packages All In One8.Node.js 微服务 All In One9.如何定位和分析 Node.js 项目中的内存泄漏问题 All In One10.Node.js process.nextTick All In One11.Web API setImmediate & Node.js setImmediate All In One12.如何使用 Node.js 和 OpenAI API 快速开发一个私有的 ChatGPT 智能聊天机器人程序 All In One13.how to config `node.js` version in vercel All In One14.Node.js & file system & async await & forEach bug All In One15.Microsoft & Node.js All In One16.Node.js fs API docs All In One17.macOS brew uninstall node.js All In One18.nvm & grep filter out only Node.js Latest LTS versions All In One19.Node.js ORM All In One20.How to exit Node.js REPL environment All In One21.Node.js 面试题 All In One22.How to custom your own Node.js Docker Image All In One23.Node.js 中 CommonJS 模块 exports 与 module.exports 实现原理剖析 All In One24.change nvm default Node.js version All In One
25.Node.js path All In One
26.如何使用 Node.js 服务器控制浏览器下载文件还是预览文件 All In One27.Node.js server render Blob file All In One28.how to exit terminal from a Node.js program All In One29.Node.js CommonJS __dirname ../ relative path bug All In One30.Node.js import ESM module error All In One31.TypeScript & Node.js crawler All In One32.Node.js process All In One33.autoprefixer: ignore next not work bug All In One34.如何判断当前 js 代码是运行在浏览器还是node环境中 All In One35.Node.js project auto open localhost with ip address All In One36.Node.js & TypeScript error All In One37.Node.js & child_process All In One38.cross-env & shelljs & set custom node.js env All In One39.nvm set system node version All In One40.Node.js 设置内存大小 All In One41.Node.js & ES Modules & TypeScript All In One42.Node.js & TS & VSCode error All In One43.Node.js & Express server support CORS44.Node.js in action All In One45.Node.js & Express.js Server get binary data All In One46.node.js & webpack proxy bug47.Node.js 实战(第2版)All In One48.node.js ECONNRESET error49.node.js cli downloader50.Node.js 文件上传 cli tools51.Node.js 实战 & 最佳 Express 项目架构52.cnblogs blogs backup & node.js crawler53.Node.js 返回 JSON 数据54.node.js 怎么扩大默认的分配的最大运行内存55.Node.js fs API _dirname & _filename & path All In One56.Node.js Backend Developer57.Node.js require 模块加载原理 All In One58.node.js module.exports & exports & module.export all in one59.Nest.js tutorials All In One60.PM2 & nodemon & Node.js Deamon All In One61.node.js 中间件62.node --experimental-modules & node.js ES Modules63.Express All In One64.Node.js & ES Modules & Jest65.how to config custom process.env in Node.js All In One66.一个最简单 node.js 命令行工具67.Node.js delete directory & file system All In One68.Node.js Learning Paths All In One69.Deno 1.0 & Node.js All In One70.Node.js & ORM & ODM All In One71.Node.js & BFF & FaaS72.Node.js 如何处理一个很大的文件 All In One73.Node.js RESTful API & EJS template engine All In One74.Node.js & ES modules & .mjs75.Node.js & 页面截图 & 生成画报 All In One76.Node.js & LTS77.Node.js Debugger All In One78.VSCode & Node.js & debugger & inspector79.玩转 Node.js & React 系列教程:graphql 教程80.Node.js & module.exports & exports All In One81.Node.js & process.env & OS Platform checker82.How to write a Node.js cli tools step by step tutorials All In One83.node.js & read argv84.Node.js & create file All In One85.Docker & Node.js86.NAIO & Node.js All In One87.node.js & fs & file read & file write All In One88.node.js & Unbuntu Linux & nvm & npm89.Node.js & SSR90.nodejs & docker91.Node.js & Koa.js All In One92.mongodb & vue & node.js93.Node.js & module system94.Node.js Spider95.How to Install Multiple Versions of Node.js on the same Server(PC) All In One96.how to updating Node.js and npm97.Node.js : npm-install 教程98.node.js && npm commands99.node.js <=> Command Line Options100.有关 Node.js, npm 和 modules 安装及使用方法的个人总结!All In OneNode.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
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16155641.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2021-04-17 shit uni-app All In One
2020-04-17 对赌协议
2019-04-17 Chromecast
2019-04-17 java & jdk All In ONe
2019-04-17 react & video player
2019-04-17 react & youtube
2016-04-17 CSS3实现 垂直居中 水平居中 的技巧