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
__dirname
and./
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等等.. 最初学习这个东西的原因很简单,在园子里 ...
随机推荐
- JVM(三)从JVM源码角度看类加载器层级关系和双亲委派
类加载器我们都知道有如下的继承结构,这个关系只是逻辑上的父子关系. 我们一直听说引导类加载器没有实体,为什么没有实体呢? 因为引导类加载器只是一段C++代码并不是什么实体类,所谓的引导类加载器就是那一 ...
- Vue3 源码之 reactivity
注: 为了直观的看到 Vue3 的实现逻辑, 本文移除了边缘情况处理.兼容处理.DEV环境的特殊逻辑等, 只保留了核心逻辑 vue-next/reactivity 实现了 Vue3 的响应性, rea ...
- uni-app请求uni.request封装使用
对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...
- IDEA SSM+MAVEN+JWT 图书管理系统
压缩包内含有MAVEN,TOMCAT,需要手动对IDEA进行配置.同时也包含数据库文件. 项目搭载了swagger,可以方便地对接口进行测试 在开发的过程中我也进行了一些记录,可以参考https:// ...
- 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.util.Scanner; 4 5 impo ...
- Centos 7 Rabbitmq 安装并开机启动
准备工作 安装wget yum install -y wget rabbitmq安装需要依赖erlang,erlang安装参考:https://www.cnblogs.com/swyy/p/11582 ...
- hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境 Hadoop Volume 配置
[手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...
- python 11 模块
模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较 ...
- 3D运动类申明与实现
#ifndef PKM3D_H #define PKM3D_H #include"kinematics.h" #include"Inventor/Qt/viewers/S ...
- Jenkins(4)docker容器内部修改jenkins容器时间
前言 用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间 查看时间 查看系统时间 date-R 进入docker容器内部,查看容器时间 docker e ...