relative path nodejs

Hello! i have a trouble with relative paths
const {created} = require(`./music/${album}/data.json`) // it works only on my computer.
const {created} = require(`./music/${album}/data.json`) // it works only on my computer.
I've tried using express, but it doesn't meet my needs ( I also entered __dirname +... but in the hosting itself, it meant /app/music/... here is my structure:
music/
100/
data.json
...
...
anotherAlbum/
data.json
...
...
index.js
music/
100/
data.json
...
...
anotherAlbum/
data.json
...
...
index.js
I looked for solutions, but I didn't find the right one. I would be very grateful if someone would help me or refer to an existing answer
Solution:
Hello! i have a trouble with relative paths
const {created} = require(`./music/${album}/data.json`) // it works only on my computer.
const {created} = require(`./music/${album}/data.json`) // it works only on my computer.
...
Jump to solution
47 Replies
Percy
Percy2y ago
Project ID: 393bf30a-2bf5-46b1-8b01-daf56f0ef6ed
zippwickness
zippwicknessOP2y ago
393bf30a-2bf5-46b1-8b01-daf56f0ef6ed
zippwickness
zippwicknessOP2y ago
Brody
Brody2y ago
does node 16 even support importing JSON like that?
zippwickness
zippwicknessOP2y ago
it appeared to be some linux or railway bug because in my repository this folder has different name, but here it is lowercase
Brody
Brody2y ago
your error shows the import path as all lowercase, what should the filename be called
zippwickness
zippwicknessOP2y ago
LoliThumb like in github repository in latest commit
Brody
Brody2y ago
sus name the import path involves string interpolation, so print the album variable on the line before the import statement, see if that is also lowercase
zippwickness
zippwicknessOP2y ago
it is "lolithumb"
Brody
Brody2y ago
loli
zippwickness
zippwicknessOP2y ago
i don't know how fs read this
Brody
Brody2y ago
I didn't say anything about fs read
zippwickness
zippwicknessOP2y ago
local it works great
Brody
Brody2y ago
that doesn't necessarily mean it's a railway specific issue try this please?
zippwickness
zippwicknessOP2y ago
i've tested it it returns with small characters because fs said that this folder have only small characters why is the LoliThumb folder written correctly on my computer, on the github repository, and probably also correctly on the server, but fs on the server searches for it with small letters, although there are a lot of similar folders with large and small letters?
Brody
Brody2y ago
good question
zippwickness
zippwicknessOP2y ago
Maybe it's stupid, but I'm surprised
Brody
Brody2y ago
I shall do some testing
zippwickness
zippwicknessOP2y ago
async function asyncLoop() {
const albums = await fs.readdirSync('./music');
const tracksPromises = [];

for (const album of albums) {
let tracks = [], images = [];
if (album !== 'data.json') {
const raw = fs.readdirSync(`./music/${album}/`);

for (const t of raw) {
const link = encodeURI(process.env.DOMAIN + `/music/${album}/` + t);
if (!isImage(`music/${t}`)) {
const promise = getAudioDurationInSeconds(`music/${album}/${t}`)
.then((duration) => {
tracks.push({ link, duration, title: t.replace(/\.[^/.]+$/, "") });
});
tracksPromises.push(promise);
} else {
images.push(t);
}
}

await Promise.all(tracksPromises);
tracksPromises.length = 0;
console.log(album)
obj[album] = {
published: dates[album].published,
relative: DateTime.fromSeconds((dates[album].published / 1000)).setLocale('en-GB').toRelativeCalendar(),
tracks: tracks,
cover: encodeURI(process.env.DOMAIN + `/music/${album}/` + images.find(x => x.replace(/\.[^/.]+$/, "") == "1")),
pic: encodeURI(process.env.DOMAIN + `/music/${album}/` + images.find(x => x.replace(/\.[^/.]+$/, "") == "2"))
}
}
}
}
async function asyncLoop() {
const albums = await fs.readdirSync('./music');
const tracksPromises = [];

for (const album of albums) {
let tracks = [], images = [];
if (album !== 'data.json') {
const raw = fs.readdirSync(`./music/${album}/`);

for (const t of raw) {
const link = encodeURI(process.env.DOMAIN + `/music/${album}/` + t);
if (!isImage(`music/${t}`)) {
const promise = getAudioDurationInSeconds(`music/${album}/${t}`)
.then((duration) => {
tracks.push({ link, duration, title: t.replace(/\.[^/.]+$/, "") });
});
tracksPromises.push(promise);
} else {
images.push(t);
}
}

await Promise.all(tracksPromises);
tracksPromises.length = 0;
console.log(album)
obj[album] = {
published: dates[album].published,
relative: DateTime.fromSeconds((dates[album].published / 1000)).setLocale('en-GB').toRelativeCalendar(),
tracks: tracks,
cover: encodeURI(process.env.DOMAIN + `/music/${album}/` + images.find(x => x.replace(/\.[^/.]+$/, "") == "1")),
pic: encodeURI(process.env.DOMAIN + `/music/${album}/` + images.find(x => x.replace(/\.[^/.]+$/, "") == "2"))
}
}
}
}
Brody
Brody2y ago
..are you hosting free music?
zippwickness
zippwicknessOP2y ago
no i'm creating some kind of portfolio
Brody
Brody2y ago
this is copyright free music?
zippwickness
zippwicknessOP2y ago
i don't know maybe yes
Brody
Brody2y ago
you cant distribute music that isnt copyright free
zippwickness
zippwicknessOP2y ago
why
Brody
Brody2y ago
please remove this functionally from your app
zippwickness
zippwicknessOP2y ago
but what about a folders?
Brody
Brody2y ago
sorry but im not gonna help you with an issue that also involves breaking railway's tos i dont say this to be rude, i just dont want your account to get banned
zippwickness
zippwicknessOP2y ago
i don't have any copyright's for these art
Brody
Brody2y ago
then you cant host it
zippwickness
zippwicknessOP2y ago
but with copyrights i can?
Brody
Brody2y ago
if you have the copyright or permission to host the music/artwork, then yes, but you dont and that means you are in clear violation of tos
zippwickness
zippwicknessOP2y ago
it's another matter thank you
Brody
Brody2y ago
thank you for understanding, please remove that content from your app
zippwickness
zippwicknessOP2y ago
done
Brody
Brody2y ago
much appreciated, i really dont mean to come off as rude, but it would be unfortunate to wake up to a banned account one day
zippwickness
zippwicknessOP2y ago
sorry for ruffling nerves and wasting your time. I really appreciate that you are helping me and even helped me avoid being banned (although I did not even suspect that I would ever encounter this) and thank you!
Brody
Brody2y ago
i will do some testing on this filename stuff, but computer is acting up real good right now files keep their casing in my testing https://test-service2.up.railway.app/
zippwickness
zippwicknessOP2y ago
can you show me the code of searching files in relative path? or it is railway's tool
Brody
Brody2y ago
there was no code involved i used https://github.com/vercel/serve
zippwickness
zippwicknessOP2y ago
wow it's useful
Brody
Brody2y ago
the code
import * as fs from 'fs';

const files = await fs.readdirSync('./data');

for (const file of files) {
console.log(file);
}
import * as fs from 'fs';

const files = await fs.readdirSync('./data');

for (const file of files) {
console.log(file);
}
Brody
Brody2y ago
the output
Brody
Brody2y ago
so this is an issue in your code somewhere
zippwickness
zippwicknessOP2y ago
okay thanks
Brody
Brody2y ago
that is definitely not how you use that
Want results from more Discord servers?
Add your server