Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1022 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nodejs absolute paths in windows with forward slash

#1
Can I have absolute paths with forward slashes in windows in nodejs? I am using something like this :

global.__base = __dirname + '/';
var Article = require(__base + 'app/models/article');

But on windows the build is failing as it is requiring something like `C:\Something\Something/apps/models/article`. I aam using webpack. So how to circumvent this issue so that the requiring remains the same i.e. `__base + 'app/models/src'`?
Reply

#2
I recommend against this, as it is patching node itself, but... well, no changes in how you require things.

(function() {
"use strict";
var path = require('path');
var oldRequire = require;
require = function(module) {
var fixedModule = path.join.apply(path, module.split(/\/|\\/));
oldRequire(fixedModule);
}
})();
Reply

#3
I finally did it like this:


var slash = require('slash');
var dirname = __dirname;
if (process.platform === 'win32') dirname = slash(dirname);

global.__base = dirname + '/';


And then to require `var Article = require(__base + 'app/models/article');`. This uses the npm package slash (which replaces backslashes by slashes in paths and handles some more cases)
Reply

#4
it's 2020, 5 years from the question was published, but I hope that for somebody my answer will be useful. I've used the replace method, here is my code(express js project):

const viewPath = (path.join(__dirname, '../views/')).replace(/\\/g, '/')

exports.articlesList = function(req, res) {
res.sendFile(viewPath + 'articlesList.html');
}
Reply

#5
I know it is a bit late to answer but I think my answer will help some visitors.

In `Node.js` you can easily get your current running file name and its directory by just using `__filename` and `__dirname` variables respectively.

In order to correct the forward and back slash accordingly to your system you can use `path` module of `Node.js`

var path = require('path');


Like here is a messed path and I want it to be correct if I want to use it on my server. Here the `path` module do everything for you
> var randomPath = "desktop//my folder/\myfile.txt";

var correctedPath = path.normalize(randomPath); //that's that

console.log(correctedPath);


> desktop/my folder/myfile.txt


If you want the absolute path of a file then you can also use `resolve` function of `path` module

var somePath = "./img.jpg";
var resolvedPath = path.resolve(somePath);

console.log(resolvedPath);

> /Users/vikasbansal/Desktop/temp/img.jpg


[1]:
Reply

#6
Use path module

const path = require("path");
var str = "test\test1 (1).txt";
console.log(str.split(path.sep)) // This is only on Windows
Reply

#7
The accepted answer doesn't actually answer the question most people come here for.
If you're looking to normalize all path separators (possibly for string work), here's what you need.

All the code segments have the node.js built-in module `path` imported to the `path` variable.
They also have the variable they work from stored in the immutable variable `str`, unless otherwise specified.

If you have a string, here's a quick one-liner normalize the string to a forward slash (/):

```js
const answer = path.resolve(str).split(path.sep).join("/");
```

You can normalize to any other separator by replacing the forward slash (/).

If you want just an array of the parts of the path, use this:

```js
const answer = path.resolve(str).split(path.sep);
```

Once you're done with your string work, use this to create a path able to be used:

```js
const answer = path.resolve(str);
```

From an array, use this:

```js
// assume the array is stored in constant variable arr
const answer = path.join(...arr);
```
Reply

#8
This is the approach I use, to save some processing:
```TypeScript
const path = require('path');

// normalize based on the OS
const normalizePath = (value: string): string {
return path.sep === '\'
? value.replace(/\\/g, '/')
: value;
}

console.log('abc/def'); // leaves as is
console.log('abc\def'); // on windows converts to `abc/def`, otherwise leave as is
```
Reply

#9
Windows uses `\`, Linux and mac use `/` for path prefixes

For Windows : `'C:\\Results\\user1\\file_23_15_30.xlsx'`

For Mac/Linux: `/Users/user1/file_23_15_30.xlsx`

If the file has `\` - it is windows, use fileSeparator as `\`, else use `/`
```
let path=__dirname; // or filePath
fileSeparator=path.includes('\')?"\":"/"
newFilePath = __dirname + fileSeparator + "fileName.csv";
```

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through