Node.js _dirname & path All In One
Node.js _dirname & path All In One
file path
相对路径
绝对路径

_dirname
https://nodejs.org/docs/latest/api/globals.html#globals_dirname
https://nodejs.org/docs/latest/api/modules.html#modules_dirname
const log = console.log;
// log(`__dirname`, __dirname);
This is the same as the path.dirname() of the __filename.
Example: running node example.js from /Users/mjr
console.log(__dirname);
// Prints: /Users/mjr
console.log(path.dirname(__filename));
// Prints: /Users/mjr
__filename
Running node example.js from /Users/mjr
console.log(__filename);
// Prints: /Users/mjr/example.js
console.log(__dirname);
// Prints: /Users/mjr
path
https://nodejs.org/api/path.html
https://github.com/nodejs/node/blob/v15.2.0/lib/path.js
const log = console.log;
const path = require('path');
// path.dirname(file_path);
let directories = path.dirname('/Users/xgqfrms/app.js');
log(directories);
// /Users/xgqfrms
- Express.js / Koa.js
- React SSR
- Vue SSR
- Electron app
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
const path = require('path');
// path.dirname(file_path);
// log(`__dirname`, __dirname);
const { app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 1000,
height: 700,
webPreferences: {
nodeIntegration: true
}
})
// win.loadFile('index.html');
// win.loadFile(__dirname + '/index.html');
// relative path 拼接
// win.loadFile(__dirname.replace(`src`, ``) + '/public/index.html');
let directories = path.dirname('index.js');
log(`directories =`, directories);
// directories = ., . 即指项目的 root path
// win.loadFile(directories.replace(`src`, ``) + '/public/index.html');
win.loadFile('./public/index.html');
// 打开 debug 模式
win.webContents.openDevTools();
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
refs
https://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js

difference between
__dirnameand./in Node.js
https://www.geeksforgeeks.org/difference-between-__dirname-and-in-node-js/
https://www.w3schools.com/nodejs/met_path_dirname.asp
process.cwd()
https://coderrocketfuel.com/article/get-the-path-of-the-current-working-directory-in-node-js
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
Node.js _dirname & path All In One的更多相关文章
- node.js之path
说到node.js,可能实际中用到node进行后台开发的公司不多,大部分人都没有开发后台的经验.但是也要了解node相关模块的用法,因为现在前端自动化脚本的构建,模块的打包越来越离不开node.特别是 ...
- node.js中path路径模块的使用
path模块是node.js中处理路径的核心模块.可以很方便的处理关于文件路径的问题. join() 将多个参数值合并成一个路径 const path = require('path'); conso ...
- Node.js:path、url、querystring模块
Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...
- node.js(四)path优化(路径优化)
1.normalize函数的基本用法 normalize函数将不符合规范的路径经过格式化转换为标准路径,解析路径中的.与..外,还能去掉多余的斜杠. 如下示例: var path = require( ...
- node.js的path模块
path模块的各种API path.join([...paths]) 参数:paths <string> ,paths参数是字符串,这些字符串按路径片段顺序排列,(A sequence o ...
- 极简 Node.js 入门 - 2.1 Path
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- Node.js 自学之旅
学习基础,JQuery 原生JS有一定基础,有自己一定技术认知(ps:原型链依然迷糊中.闭包6不起来!哎!) 当然最好有语言基础,C#,java,PHP等等.. 最初学习这个东西的原因很简单,在园子里 ...
- [Node.js] Serve Static Files with Express
In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Exp ...
- Node.js 自学之旅(初稿篇)
学习基础,JQuery 原生JS有一定基础,有自己一定技术认知(ps:原型链依然迷糊中.闭包6不起来!哎!) 当然最好有语言基础,C#,java,PHP等等.. 最初学习这个东西的原因很简单,在园子里 ...
随机推荐
- 06. struts2中指定struts2处理的请求后缀
概述 默认情况下我们都是使用.action后缀访问Action. 其实默认后缀是可以通过常量"struts.action.extension"进行修改的. 我们可以配置Struts ...
- 关于Vue v-model你需要知道的一切
v-model是Vue的一个指令,它提供了input和form数据之间或两个组件之间的双向数据绑定. 这在Vue开发中是一个简单的概念,但是v-model的真正威力需要一些时间才能理解. 到本教程结 ...
- wireshark使用手册
Wireshark的过滤器 使用Wireshark时最常见的问题,是当您使用默认设置时,会得到大量冗余信息,以至于很难找到自己需要的部分. 过犹不及. 这就是为什么过滤器会如此重要.它们可以帮助我们在 ...
- SVN、GIT比较
Git是分布式的,完全可以不备份代码,下载下来后,在本地不必联网就可以看到所有的log,跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并,也可以不用联网在本地提交 SVN ...
- Vue3(三)CND + ES6的import + 工程化的目录结构 = 啥?
突发奇想 这几天整理了一下vue的几种使用方式,对比之后发现有很多相似之处,那么是不是可以混合使用呢?比如这样: vue的全家桶和UI库,采用传统的方式加载(CND.script). 自己写的js代码 ...
- eclipse 断点调试方法
1 Debug视图 1.1 线程堆栈视图 线程堆栈视图表示当前线程的堆栈,从中可以看出在运行哪些代码,并且整个调用过程,以及代码行号.分别介绍一下这几个按钮的含义.从左至右分别为: 1.表示当前实现继 ...
- JavaWeb——EL及JSTL学习总结
什么是EL表达式 为什么需要EL EL的主要作用 EL的语法 EL的开发步骤 EL实例练习 EL中的运算符 EL表达式显示内容的特点 EL的特点 EL隐式对象 EL隐式对象介绍 隐式对象实例练习 什么 ...
- 压缩文件 .zip.001 .zip.002合并
可以把名字特别长的命名为1 这样简单些 copy /B 1.zip.001+1.zip.002 1.zip
- sql 工具类function
--判断是否为整数 create or replace function is_number(param VARCHAR2) return NUMBER is v_num NUMBER; begin ...
- sql,关键字使用
select instr('dds万','万',1) from dual --判断万关键字是否存在 select to_single_byte('9') from dual --全角数字转为半角数字 ...