Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zerophreak's avatar

Compiling node assets on server

Hi all, so far I've always been compiling assets locally, pushed them in my branch and deployed. But I'd like to prevent myself those issues and compile them on the server. Environment handled by default setup of Laravel Forge & Envoyer.

Questions:

  1. Do you recommend running "npm run production" on the server?

  2. Will it run out of the box on a Forge-deployed server?

  3. At what step in the deployment should this run?

Thanks in advance!

0 likes
16 replies
ahmeddabak's avatar

Do you recommend running "npm run production" on the server?

No, please do not do that on production server, npm will install about 23000 files on the server just to compile the code.

Will it run out of the box on a Forge-deployed server?

it seems that you can not source

1 like
zerophreak's avatar

Thank you for your quick response. I am wondering why else it would not be recommended to run it on the server? You seem to find both sides in the web... Looking forward to some insights!

jprangenbergde's avatar

The only correct way is to run npm run production in the build script on your build server, before the deployment process.

You have just to commit the package-lock.json. Same procedure like with the composer.lock!

fylzero's avatar

@zerophreak I was curious about this too and couldn't find a great answer. I found a way to automate it for myself with Envoy... but that only works because I am the sole dev on these projects.

You can see the thread here.

https://laracasts.com/discuss/channels/laravel/deployment-question-regarding-running-npm-run-prod-on-deploy

I'm still curious what the proper procedure for doing this is as it seems things get tricky when deploying. Before I basically had to remember to compile as prod before every deployment. That seems wrong.

I also found this thread that seems to mirror the concern...

https://laracasts.com/discuss/channels/elixir/when-to-use-npm-run-production

1 like
zerophreak's avatar

@fylzero Thank you for your reply! Indeed, we still don’t really have a conclusion. Frankly you can find people supporting both use cases. It seems to be depending on many aspects each way but it seems there is no golden standard...

fylzero's avatar

@zerophreak I actually want an answer to this to. I have an idea of an approach that I will mess with today and let you know what I find.

One thing you should try... run npm ci before running npm run production in your build script. From what I've read that should work.

@bobbybouwmann suggested this approach here...

https://laracasts.com/discuss/channels/elixir/npm-run-prod-commits-and-forge

I think running this on the production server sounds fine. If it fails, it just won't build and you'll see this failure reflected in the build.

@jprangenbergde Are you saying that we should have a completely separate server for just building the project? I'm not sure I am following what you are saying. What exactly is a "build server" in this use case? I'm just not familiar with this.

fylzero's avatar

@sinnbeck With that said... are you opposed to having the npm run production command execute on the production server?

I've actually been trying to figure out the best place to stick this for a while. I also use Envoy.

Sinnbeck's avatar

@fylzero Personally not. There not many users so it does not slow don't. And i only have a max of 5 builds, meaning it does not fill alot. Been doing it for 6 months and haven't had one compliant.

Also the fact that spatie does it, is for me a sign that it is battle tested

fylzero's avatar

@zerophreak I've run into a thing with this before, which is why I stopped doing it... but I think I may have figured out the issue.

If this works, I agree with @sinnbeck that doing this on the server should be perfectly fine / battle tested as he said.

fylzero's avatar
fylzero
Best Answer
Level 67

@zerophreak Ok, did a test... this works perfectly fine and I will be adopting this in my process...

So long as you run npm ci BEFORE running npm run production in your build script. It works fine!

That should be your answer. Yes, you can do it and it is fine to do. As far as where to run it... just put it after npm ci

Here's my current deploy script for Envoy...

@servers(['www' => ['serverpilot@my-ip-address']])

@task('deploy', ['on' => 'www'])
    cd ~/apps/my-app
    php artisan down --message="Deploying New Code"
    git pull origin master
    composer install --optimize-autoloader --no-dev
    php artisan config:clear
    php artisan config:cache
    php artisan route:clear
    php artisan route:cache
    npm ci
    npm run production
    php artisan migrate --force
    php artisan up
@endtask

I literally just updated like 10 deployment scripts with this. I'm really happy this works. πŸ˜„

@snapey I like the thought of using Github actions but I am personally more comfortable with this approach in the meantime.

1 like
zerophreak's avatar

Thank you all.

@fylzero I mean of course this should work but for me e.g. it just times out in the Envoyer deployment process. Runs for 3 mins and then it's over. Even on my local machine it only takes a minute to compile... Doesn't feel comforting πŸ˜…

Please or to participate in this conversation.