Skip to content

Instantly share code, notes, and snippets.

@maxfunke
Created February 2, 2017 09:15
Show Gist options
  • Save maxfunke/63bb9c161adfb8597437f4dfe17af2db to your computer and use it in GitHub Desktop.
Save maxfunke/63bb9c161adfb8597437f4dfe17af2db to your computer and use it in GitHub Desktop.
Handling paths for different OS in Node.js - '/' or '\'
'use strict';
const FS = require('fs');
const OS = require('os');
let EXEC_PATH = process.argv[1];
let FILE_PATH = getFilePath(EXEC_PATH);
const FILE = JSON.parse(FS.readFileSync(FILE_PATH + '/myFile.json', 'utf8'));
function getFilePath(execPath){
let splitter = (OS.type() === "Windows_NT") ? "\\" : "/";
let path = execPath.split(splitter);
path.pop();
return (path.join(splitter));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment