R
Railwayā€¢8mo 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ā€¢8mo ago
Project ID: a3d167c9-773f-4271-a6bc-99089387a3b6
samp
sampā€¢8mo 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ā€¢8mo ago
is the messages.json in your root folder? mind showing me your filetree?
samp
sampā€¢8mo ago
No description
samp
sampā€¢8mo ago
in my local project folder
Fragly
Fraglyā€¢8mo 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
sampā€¢8mo 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ā€¢8mo ago
oh you're right, my fault
samp
sampā€¢8mo 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ā€¢8mo ago
I've never had issues with __dirname šŸ¤”
samp
sampā€¢8mo 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ā€¢8mo ago
You definitally can use file system functions
Solution
samp
sampā€¢8mo ago
alright, I fixed it by hosting messages.json in a github gist rather than in the project files