R
Railwayā€¢12mo ago
samp

Railway-specific error

It works fine when running locally: Error: ENOENT: no such file or directory, open '/app/build/messages.json'
Solution:
alright, I fixed it by hosting messages.json in a github gist rather than in the project files
Jump to solution
13 Replies
Percy
Percyā€¢12mo ago
Project ID: a3d167c9-773f-4271-a6bc-99089387a3b6
samp
sampOPā€¢12mo ago
a3d167c9-773f-4271-a6bc-99089387a3b6 i think it's probably because of this part of the code:
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
i used dirname for message.json: ```js const jsonData = JSON.parse(readFileSync(join(dirname, 'messages.json'), 'utf8')); ``` like that maybe that doesn't work with railway? maybe __dirname isn't good in railway? what's the alternative then?
Fragly
Fraglyā€¢12mo ago
is the messages.json in your root folder? mind showing me your filetree?
samp
sampOPā€¢12mo ago
No description
samp
sampOPā€¢12mo ago
in my local project folder
Fragly
Fraglyā€¢12mo ago
When you compile your typescript to javascript, it'll take every .ts file in your src folder and create the .js version in build since your messages.json is not a .ts file, it's ignored by the builder and is not sent to build This means when you run your project in typescript then it'll work because the messages.json file is present, but when you run in production with javascript, then it won't work because messages.json is not present in your build folder To fix this, you'd want to move your messages.json file to the root directory and then access it with:
import path from "path"

const pathToMessages = path.join(process.cwd(), "messages.json")
import path from "path"

const pathToMessages = path.join(process.cwd(), "messages.json")
that way you can access it both from your typescript dev folder and your production folder
samp
sampOPā€¢12mo ago
no, the json file does appear in build.
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",

"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"outDir": "build",
},

"include": ["src/**/*.ts", "src/**/*.json"]
}
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",

"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"outDir": "build",
},

"include": ["src/**/*.ts", "src/**/*.json"]
}
it works when run locally, but not when it's on railway
Fragly
Fraglyā€¢12mo ago
oh you're right, my fault
samp
sampOPā€¢12mo ago
i think it's probably because __dirname doesn't work on railway for some reason maybe file system is different or something
Fragly
Fraglyā€¢12mo ago
I've never had issues with __dirname šŸ¤”
samp
sampOPā€¢12mo ago
__dirname looks like this
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
since apparently __dirname is only for commonjs modules or something maybe railway's file system is weird idk ah, i've got it it's because you can't use file system functions in something like railway apparently since it's a web-based environment or something
Fragly
Fraglyā€¢12mo ago
You definitally can use file system functions
Solution
samp
sampā€¢12mo ago
alright, I fixed it by hosting messages.json in a github gist rather than in the project files
Want results from more Discord servers?
Add your server