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

fylzero's avatar
Level 67

Deployment question regarding running `npm run prod` on deploy.

I use Envoy to deploy to a Digital Ocean server and was thinking it would be nice if npm run prod would automatically fire during deployment.

I tried running this directly on the server but the server is missing cross-env, probably laravel-mix, etc npm packages...

I'm considering moving those to regular dependencies instead of dev dependencies... is there any harm in that? Does that sound like a bad idea? Totally standard and go for it?

Curious what the consensus is out there.

0 likes
8 replies
fylzero's avatar
Level 67

@bugsysha That is for continuous integration and more for the installing of packages not the build process. I think I currently use npm install --production or something close to that to handle the install.

I'm trying to avoid having to run npm run prod locally to compile js and css before deploying.

I'm thinking that should just build on the server but idk.

Am I misunderstanding what npm ci does? Would you mind providing a bit more context please?

3 likes
bugsysha's avatar
bugsysha
Best Answer
Level 61

I use envoyer.io to deploy projects with zero downtime. One of the steps is npm ci. All compiled assets are excluded from git and everything is built on deployment for production and CI with npm run prod.

So first I do npm ci then it is followed by npm run prod.

Let me try to answer in chunks so maybe my logic will make more sense.

I use Envoy to deploy to a Digital Ocean server and was thinking it would be nice if npm run prod would automatically fire during deployment.

envoyer.io does this so I do not have anything else to recommend. It can be done also on Laravel Forge but zero downtime is quite to my liking.

I tried running this directly on the server but the server is missing cross-env, probably laravel-mix, etc npm packages... I'm considering moving those to regular dependencies instead of dev dependencies... is there any harm in that? Does that sound like a bad idea? Totally standard and go for it?

For me cross-env is in devDependencies and I never had this issue. But the reason I've mentioned npm ci is that maybe it can solve some of your deployment build issues since it helped me few times. I do not recall if it was for that same issue you are having right now but it is worth a shot.

That is for continuous integration and more for the installing of packages not the build process. I think I currently use npm install --production or something close to that to handle the install.

You are right. But there are differences in ci and install so I wanted to point that out. You can read all about differences here https://docs.npmjs.com/cli/ci

I'm trying to avoid having to run npm run prod locally to compile js and css before deploying.

Understood. You can do that on envoyer.io or forge.laravel.com

I'm thinking that should just build on the server but idk.

Yes it should. You can easily do that with deployment hooks on envoyer.io or deploy script on forge.laravel.com

Am I misunderstanding what npm ci does? Would you mind providing a bit more context please?

Read everything above 🤣

Kidding aside you are not missing something. Just pointing out npm ci cause not so many know about it, and didn't commented much about npm run prod since you can easily run it on production with mentioned two deployment tools.

In short, the main differences between using npm install and npm ci are:

  • The project must have an existing package-lock.json or npm-shrinkwrap.json.
  • If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock.
  • npm ci can only install entire projects at a time: individual dependencies cannot be added with this command.
  • If a node_modules is already present, it will be automatically removed before npm ci begins its install.
  • It will never write to package.json or any of the package-locks: installs are essentially frozen.kk
2 likes
fylzero's avatar
Level 67

@bugsysha Ok, I see part of the disconnect maybe.

I'm not using Envoyer... I'm using Envoy. Those are different things altogether. I don't use Forge and have no intention to move to Forge.

So, my entire question is... using Envoy, not Envoyer, can I (and should I) run npm run prod on my server so I don't have to do this locally?

I'm imagining I should as that seems to make sense as part of the build process for prod.

...but my question has nothing to do with npm install or npm ci unless the ci command somehow runs webpack for prod.

Envoyer and Forge aren't just deployment tools they are entire server management platforms that I'm not interested in switching to. I need more flexibility on my server environment as I run multiple platforms.

2 likes
bugsysha's avatar

Ok, I see part of the disconnect maybe.

No disconnect, just saying that any modern deployment tool should allow you to run which ever commands you want during deployment process and that there is nothing wrong with compiling assets during that same deployment process.

...but my question has nothing to do with npm install or npm ci unless the ci command somehow runs webpack for prod.

npm ci can maybe help fix some issues with build process so I would default to that instead of npm install or npm install --production.

1 like
fylzero's avatar
Level 67

@bugsysha While I appreciate your insight, this is kind of all steering away from the question I was asking.

I have no interest in moving to Forge, Envoyer and I already understand what npm ci is for. I don't care much about using it because I always update packages to latest and am fine with that riding off my package.json and not the lock file. At least for now. I may ultimately wind up moving to Vapor which would be a different story.

That said, in this moment... just trying to figure out how I can skip the local build step of running webpack for prod locally and add it to my current deployment.

I'd imagine this probably isn't possible with Envoy since deployment relies on a server end git pull... if I run build there it would be ahead of my local master and break as well.

2 likes
fylzero's avatar
Level 67

I found a solution to the problem... using "story" I can execute the build command locally without having to remember to do this manually every time...

Here is my new Envoy.blade.php

@servers(['localhost' => '127.0.0.1', 'www' => ['[email protected]']])

@story('deploy')
    build
    deployment
@endstory

@task('build', ['on' => 'localhost'])
    npm run prod
    git add . // Probably not super smart, but it'll work for now.
    git commit -m 'Ran npm run prod'
    git push
@endtask

@task('deployment', ['on' => 'www'])
    cd ~/apps/my-app-folder
    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 install --production
    php artisan migrate --force
    php artisan up
@endtask

@bugsysha Unlike that other guy, I at least give credit for participating as I actually appreciate the help.

1 like
bugsysha's avatar

I found a solution to the problem... using "story" I can execute the build command locally without having to remember to do this manually every time...

Nice.

Unlike that other guy, I at least give credit for participating as I actually appreciate the help.

Thanks you very much.

2 likes

Please or to participate in this conversation.