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
Percy14mo ago
Project ID: 393bf30a-2bf5-46b1-8b01-daf56f0ef6ed
zippwickness
zippwickness14mo ago
393bf30a-2bf5-46b1-8b01-daf56f0ef6ed
zippwickness
zippwickness14mo ago
Brody
Brody14mo ago
does node 16 even support importing JSON like that?
zippwickness
zippwickness14mo 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
Brody14mo ago
your error shows the import path as all lowercase, what should the filename be called
zippwickness
zippwickness14mo ago
LoliThumb like in github repository in latest commit
Brody
Brody14mo 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
zippwickness14mo ago
it is "lolithumb"
Brody
Brody14mo ago
loli
zippwickness
zippwickness14mo ago
i don't know how fs read this
Brody
Brody14mo ago
I didn't say anything about fs read
zippwickness
zippwickness14mo ago
local it works great
Brody
Brody14mo ago
that doesn't necessarily mean it's a railway specific issue try this please?
zippwickness
zippwickness14mo 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
Brody14mo ago
good question
zippwickness
zippwickness14mo ago
Maybe it's stupid, but I'm surprised
Brody
Brody14mo ago
I shall do some testing
zippwickness
zippwickness14mo 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
Brody14mo ago
..are you hosting free music?
zippwickness
zippwickness14mo ago
no i'm creating some kind of portfolio
Brody
Brody14mo ago
this is copyright free music?
zippwickness
zippwickness14mo ago
i don't know maybe yes
Brody
Brody14mo ago
you cant distribute music that isnt copyright free
zippwickness
zippwickness14mo ago
why
Brody
Brody14mo ago
please remove this functionally from your app
zippwickness
zippwickness14mo ago
but what about a folders?
Brody
Brody14mo 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
zippwickness14mo ago
i don't have any copyright's for these art
Brody
Brody14mo ago
then you cant host it
zippwickness
zippwickness14mo ago
but with copyrights i can?
Brody
Brody14mo 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
zippwickness14mo ago
it's another matter thank you
Brody
Brody14mo ago
thank you for understanding, please remove that content from your app
zippwickness
zippwickness14mo ago
done
Brody
Brody14mo 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
zippwickness14mo 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
Brody14mo 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
zippwickness14mo ago
can you show me the code of searching files in relative path? or it is railway's tool
Brody
Brody14mo ago
there was no code involved i used https://github.com/vercel/serve
zippwickness
zippwickness14mo ago
wow it's useful
Brody
Brody14mo 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
Brody14mo ago
the output
Brody
Brody14mo ago
so this is an issue in your code somewhere
zippwickness
zippwickness14mo ago
okay thanks
Brody
Brody14mo ago
that is definitely not how you use that