[RESOLVED] package.json's script, how to run 'prepare' and then build ?
In my package.json file, I use the script "prepare" to dynamically create a "version.ts" file (a simple file which export a constant with the version number read from the package.json's version itself) =>
"scripts": {
"prepare": "node -p "'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'" > ./app/javascript/sources/version.ts" } On Heroku, it works perfectly: the file "version.ts" is created on the fly upon deployment, and my app can import LIB_VERSION. However it does not on Railway.app; I get an error telling me that it cannot find "version.ts". Any idea why ? Many thanks !
"prepare": "node -p "'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'" > ./app/javascript/sources/version.ts" } On Heroku, it works perfectly: the file "version.ts" is created on the fly upon deployment, and my app can import LIB_VERSION. However it does not on Railway.app; I get an error telling me that it cannot find "version.ts". Any idea why ? Many thanks !
17 Replies
Hello - just a bump on my question... no one from Railway.app to look at my question ? Maybe I am not in the right forum ?
worlds most patient man.
Umm that looks like a script you run pre deploy / as a build step. you can set the start command to be
npm run prepare && <start command>
or better set a custom build command that runs npm run prepare
@suzume hope this helps?@Finn many thanks for your response! I had given up hope so I did not check back in discord for quite a while, hence my late reply 🙂 . Now, it did help but it doesn't work quite yet. Problem is, I am a complete noob when it comes to npm / yarn / package.json. So as shown in my excerpt above, I have only 1 script, "prepare". So if I edit the build settings of my railway.app to "yarn run prepare", it does indeed run the "prepare" script... but it stops there, it does not compile my whole vue app. How can I run "prepare" and then continue with the normal flow, i.e. compile the rest of my javascript ?
yea you can use
&& < normal build command>
if that make senseThat was quick! So my problem is, I do not have a "normal build command". I just let Heroku (and now Railway) do their stuff. I have a simple package.json with only 1 "prepare" script which Heroku seems to run before any build command of its own, and which Railway doesn't. So after "yarn run prepare", I have nothing else to run. If I delete the "prepare" script, remove any reference to "LIB_VERSION" in my code, and do not edit the "build command" in the settings of Railway, Railway is able to deploy my app without any issue. I do not know what kind of script or call it does by default, hence I do not know what to put after "yarn run prepare && xxxxx" 😕
The start or build command ig? Idk what yarn prepar does
Sorry, I guess I was not clear. First, "yarn run prepare" is, I guess, equivalent to "npm run prepare" (I use yarn and not npm). What I am trying to say is that if I remove the "prepare" script, Railway is able to deploy automatically without any issue. When I add a "prepare" script, Railway does not run it by default before starting to compile the javascript files - which is problematic, as I need to have this script run first. So to run "prepare" script first, I did what you suggested: edit the Railway settings under "build settings" and put "yarn run prepare" ... and it worked, it ran the "prepare" script, but nothing else. So now I have to run another script, I assume, to compile my javascript files and use the command "yarn run prepare && build" - but I do not have a "build" script. When I deploy with Heroku, I don't need such script, Heroku knows what to do. And when I deploy under Railway (without the "prepare" script), Railway also knows what to do, it does not need a "build" script. Sorry, long explanation...
In other words, I would like to add a "build" script, I just don't know what to put in it.
Like I've said
yarn prepare && < your build command>
as a build ckmmand5
Command*
Unless I'm missing somethingI don’t understand what you mean by “ckmmand5”, sorry. I can run “yarn run prepare”, but I don’t know what kind of command I should put after “ && “. I understand you tell me to put a <build command>, but I do not know what command this is, as usually, if I didn’t have a “prepare” script, I wouldn’t have to put any <build command> at all (Heroku / Railway does something automatically but I do not know what it is they run).
i missspelled command. is your project opensource, i cant guess the build command
No, sorry it is not open source. In parallel I am trying to reach out to Railway support directly for this issue. What I can tell additionally is that my app runs on Rails 7 with a Vue front end. Not sure it helps with anything. Sorry Finn, not sure what else to share or do… but anyway, thanks for the time spent trying to help me out, really appreciate it. I will update this thread if I somehow fix my problem.
Flagging this thread. A team member will be with you shortly.
Hey @suzume - got your email. Adding this to the list of projects we need to fix.
thanks @Angelo
Hi @suzume,
Can you try changing the build command to
This will run prepare first before compiling rails assets
Fantastic, it works ! Thanks a ton 🙂 The strangest thing happened though: I executed the command "railway up" and on the railway UI I could see that two deployments started at the same time (one was just 5 seconds behind). No idea why. But anyway, one of the deployment failed, and the other one worked. Just to be sure, I deployed again with "railway up", and this time only 1 deployment started, and it worked perfectly. Thanks again for your help.
❤️