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

fitdut's avatar
Level 19

When to use `npm run production`

With regards to front end dependencies I understand that the general practice is to install them into devDependencies and then concatenate them all into one file which will then be pushed with version control.

I was always under the impression that the npm run production command should be run on the production server but that makes no sense if none of the dependecies is installed in production.

Does that mean you run the command just before you commit and push to production? This seems inefficient to me. Am I missing the point?

0 likes
3 replies
Swaz's avatar
Swaz
Best Answer
Level 20

I've played around with having npm run production as part of my server deployment script, but it's just another thing that can go wrong on your server. Plus you have to deal with having all that extra stuff installed on your server.

I think it's easier to just run it locally before you deploy, but obviously everyones situation is different.

3 likes
jarnheimer's avatar

It would be very automate this before each deploy since I don't want to push the minified code to the repository. Anyone using a Deployment Hook on Envoyer for this? What is the best practice?

4 likes
Nicholaus's avatar

I set up an aliased command that runs a shell script that runsnpm run production on my development server prior to pushing the code to my git repository.

That way when I deploy the code to production, the production server doesn't have to do npm stuff.

GitNpmProdShellScript.sh:

#!/bin/sh
# Runs 'npm run production' to compile assets
# Then runs git add, commit and push

# Grab the git commit message
commitMessage=$1

# Quit if no commit message
if [ -z "$commitMessage" ]; then 
    echo "Forgot to include commit message"
    exit 1;
fi

# Compile to production and add to commit message
npm run production

# Add, commit and push!
git add .
git commit -m "$commitMessage"
git push

.bash_aliases:

alias gitnpmprodpush="bash ~/GitNpmProdShellScript.sh"
8 likes

Please or to participate in this conversation.