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

Node.js CommonJS __dirname ../ relative path bug All In One

Node.js CommonJS __dirname ../ relative path bug All In One

CommonJS

__dirname + ../

var fs = require("fs");

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'

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 ./src/mkdir.cjs

*/

path.resolve

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
// }


/*

node ./src/mkdir.cjs

*/


ESM

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


const log = console.log;

// 默认当前 root 路径 ✅
const __dirname = path.resolve();

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

// const dir = path.resolve(__dirname, '/upload');
// ❌ Error: EROFS: read-only file system, mkdir '/upload'

// ../ 当前文件上一层路径下创建文件夹 ✅  (./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);
// esm 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
// }


/*

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

node ./src/mkdir.js

*/


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

*/


demo

{
  "name": "crawler",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node ./src/app.ts",
    "app": "tsc ./src/app.ts",
    "mk": "node ./src/mkdir.js",
    "mk-src": "node ./src/mkdir-src.js",
    "cjs": "node ./src/mkdir.cjs",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type-esm": "module",
  "type": "module",
  "author": "xgqfrms",
  "license": "MIT",
  "devDependencies": {
    "@types/superagent": "^4.1.15",
    "cheerio": "^1.0.0-rc.10",
    "superagent": "^7.1.2",
    "ts-node": "^10.7.0"
  }
}

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

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

$ node ./src/mkdir.cjs

node.js url

import.meta.url & url.fileURLToPath

import * as url from 'url';

const log = console.log;

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

log('esm __filename =', __filename);
// esm __filename = /Users/xgqfrms-mbp/Documents/GitHub/app/src/url.js

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

log('import.meta.url =', import.meta.url);
// import.meta.url = /Users/xgqfrms-mbp/Documents/GitHub/app/src/url.js


const root_dirname = url.fileURLToPath(new URL('../', import.meta.url));
const outer_dirname = url.fileURLToPath(new URL('../../', import.meta.url));

log('root_dirname =', root_dirname);
// import.meta.url = /Users/xgqfrms-mbp/Documents/GitHub/app/

log('outer_dirname =', outer_dirname);
// import.meta.url = /Users/xgqfrms-mbp/Documents/GitHub/





API

https://nodejs.org/api/fs.html#fsmkdirsyncpath-options

https://nodejs.org/api/fs.html#fsmkdirpath-options-callback

https://nodejs.org/api/fs.html#fsexistssyncpath

https://nodejs.org/api/fs.html#fsexistspath-callback

refs

https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules

https://stackoverflow.com/questions/8817423/why-is-dirname-not-defined-in-node-repl

https://blog.jim-nielsen.com/2019/common-js-equivalent-of-dirname-in-es-modules/

https://www.cnblogs.com/xgqfrms/p/16099883.html



©xgqfrms 2012-2020

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

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


posted @ 2022-04-04 18:53  xgqfrms  阅读(60)  评论(9编辑  收藏  举报