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

  1. const log = console.log;
  2. // log(`__dirname`, __dirname);

This is the same as the path.dirname() of the __filename.

Example: running node example.js from /Users/mjr

  1. console.log(__dirname);
  2. // Prints: /Users/mjr
  3. console.log(path.dirname(__filename));
  4. // Prints: /Users/mjr

__filename

Running node example.js from /Users/mjr

  1. console.log(__filename);
  2. // Prints: /Users/mjr/example.js
  3. console.log(__dirname);
  4. // Prints: /Users/mjr

path

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

https://github.com/nodejs/node/blob/v15.2.0/lib/path.js

  1. const log = console.log;
  2. const path = require('path');
  3. // path.dirname(file_path);
  4. let directories = path.dirname('/Users/xgqfrms/app.js');
  5. log(directories);
  6. // /Users/xgqfrms
  1. Express.js / Koa.js

  1. React SSR

  1. Vue SSR

  1. Electron app

  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-11-01
  8. * @modified
  9. *
  10. * @description
  11. * @difficulty Easy Medium Hard
  12. * @complexity O(n)
  13. * @augments
  14. * @example
  15. * @link
  16. * @solutions
  17. *
  18. * @best_solutions
  19. *
  20. */
  21. const log = console.log;
  22. const path = require('path');
  23. // path.dirname(file_path);
  24. // log(`__dirname`, __dirname);
  25. const { app, BrowserWindow } = require('electron')
  26. function createWindow () {
  27. const win = new BrowserWindow({
  28. width: 1000,
  29. height: 700,
  30. webPreferences: {
  31. nodeIntegration: true
  32. }
  33. })
  34. // win.loadFile('index.html');
  35. // win.loadFile(__dirname + '/index.html');
  36. // relative path 拼接
  37. // win.loadFile(__dirname.replace(`src`, ``) + '/public/index.html');
  38. let directories = path.dirname('index.js');
  39. log(`directories =`, directories);
  40. // directories = ., . 即指项目的 root path
  41. // win.loadFile(directories.replace(`src`, ``) + '/public/index.html');
  42. win.loadFile('./public/index.html');
  43. // 打开 debug 模式
  44. win.webContents.openDevTools();
  45. }
  46. app.whenReady().then(createWindow)
  47. app.on('window-all-closed', () => {
  48. if (process.platform !== 'darwin') {
  49. app.quit()
  50. }
  51. })
  52. app.on('activate', () => {
  53. if (BrowserWindow.getAllWindows().length === 0) {
  54. createWindow()
  55. }
  56. })

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的更多相关文章

  1. node.js之path

    说到node.js,可能实际中用到node进行后台开发的公司不多,大部分人都没有开发后台的经验.但是也要了解node相关模块的用法,因为现在前端自动化脚本的构建,模块的打包越来越离不开node.特别是 ...

  2. node.js中path路径模块的使用

    path模块是node.js中处理路径的核心模块.可以很方便的处理关于文件路径的问题. join() 将多个参数值合并成一个路径 const path = require('path'); conso ...

  3. Node.js:path、url、querystring模块

    Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...

  4. node.js(四)path优化(路径优化)

    1.normalize函数的基本用法 normalize函数将不符合规范的路径经过格式化转换为标准路径,解析路径中的.与..外,还能去掉多余的斜杠. 如下示例: var path = require( ...

  5. node.js的path模块

    path模块的各种API path.join([...paths]) 参数:paths <string> ,paths参数是字符串,这些字符串按路径片段顺序排列,(A sequence o ...

  6. 极简 Node.js 入门 - 2.1 Path

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  7. Node.js 自学之旅

    学习基础,JQuery 原生JS有一定基础,有自己一定技术认知(ps:原型链依然迷糊中.闭包6不起来!哎!) 当然最好有语言基础,C#,java,PHP等等.. 最初学习这个东西的原因很简单,在园子里 ...

  8. [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 ...

  9. Node.js 自学之旅(初稿篇)

    学习基础,JQuery 原生JS有一定基础,有自己一定技术认知(ps:原型链依然迷糊中.闭包6不起来!哎!) 当然最好有语言基础,C#,java,PHP等等.. 最初学习这个东西的原因很简单,在园子里 ...

随机推荐

  1. JVM(三)从JVM源码角度看类加载器层级关系和双亲委派

    类加载器我们都知道有如下的继承结构,这个关系只是逻辑上的父子关系. 我们一直听说引导类加载器没有实体,为什么没有实体呢? 因为引导类加载器只是一段C++代码并不是什么实体类,所谓的引导类加载器就是那一 ...

  2. Vue3 源码之 reactivity

    注: 为了直观的看到 Vue3 的实现逻辑, 本文移除了边缘情况处理.兼容处理.DEV环境的特殊逻辑等, 只保留了核心逻辑 vue-next/reactivity 实现了 Vue3 的响应性, rea ...

  3. uni-app请求uni.request封装使用

    对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...

  4. IDEA SSM+MAVEN+JWT 图书管理系统

    压缩包内含有MAVEN,TOMCAT,需要手动对IDEA进行配置.同时也包含数据库文件. 项目搭载了swagger,可以方便地对接口进行测试 在开发的过程中我也进行了一些记录,可以参考https:// ...

  5. 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件

    1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.util.Scanner; 4 5 impo ...

  6. Centos 7 Rabbitmq 安装并开机启动

    准备工作 安装wget yum install -y wget rabbitmq安装需要依赖erlang,erlang安装参考:https://www.cnblogs.com/swyy/p/11582 ...

  7. hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境 Hadoop Volume 配置

    [手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...

  8. python 11 模块

    模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较 ...

  9. 3D运动类申明与实现

    #ifndef PKM3D_H #define PKM3D_H #include"kinematics.h" #include"Inventor/Qt/viewers/S ...

  10. Jenkins(4)docker容器内部修改jenkins容器时间

    前言 用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间 查看时间 查看系统时间 date-R 进入docker容器内部,查看容器时间 docker e ...