10

Laravel ships with a package.json file for npm.

The default package.json only ships with devDependencies.

If I am not mistaken then:

  • npm run development is used to bundle all dependencies with web pack into a single file on local developent, which is then pushed via version-control to production.
  • npm install is only required on production, when package.json contains actual dependencies (and not only devDependencies).

However, I am a bit confused about the npm run production script. Should I run webpack in production? Or is this obsolete if I have done this in development and pushed it via VC? I did not find anything in the Deploy docs from Laravel.

Adam
  • 15,564
  • 13
  • 95
  • 165
  • 3
    I'm not sure about the specifics of Laravel's scripts, but generally `development` does a development build and you do NOT put these build files under version control. `production` likewise would do a production build -- you deploy the files that it builds to your server, but you don't need to run the script itself in prod. – mpen Nov 26 '19 at 19:39
  • @mpen do you mean `npm run production` should not be run on the production server? – Kerkouch Nov 26 '19 at 19:45
  • 1
    @Kerkouch I agree completely with mpen's answer. You run them locally, not on the server. – nakov Nov 26 '19 at 19:48
  • 2
    @Kerkouch Correct. If your app is PHP only, you don't even need node or npm installed on your production server. package.json [non-dev] `dependencies` is really more for node applications, where half the dependencies are only needed for development, and you actually need the other half installed on the production server to run your app. But Laravel/PHP apps typically pre-compile all the assets before deploying. – mpen Nov 26 '19 at 21:38
  • The link to the Laravel package json doesn't work (any more). Here is the one for Laravel 8: https://github.com/laravel/laravel/blob/8.x/package.json – Frank Lämmer Feb 19 '21 at 16:55

1 Answers1

4

What I usually do is use npm run dev or npm run watch which just watches for changes and still does development compiling, which means any console.log that I use, and the output is not minified, so this is good for development purposes as the scripts says :) . Before I push to production I ran npm run prod which then minifies the output and I version the output for caching purposes:

https://laravel.com/docs/master/mix#versioning-and-cache-busting

And I forgot to mention about the installing part.. if you run npm install on production it will install the devDependencies as well. So check this answer

https://stackoverflow.com/a/9276112/1457270

nakov
  • 12,121
  • 6
  • 26
  • 75
  • So the only `npm` command you run on the production server is `npm install --production`, right? – Adam Nov 26 '19 at 20:09
  • @Adam, I don't run even that one, as I compile the assets locally. So I usually compile one `libs.js` scripts which includes most of the third party libraries used, and then `app.js` which is my own scripts/components. And I push them minified to my repository as well, so the team shares the same versions. – nakov Nov 26 '19 at 20:28
  • My problem with compiling everything locally is that ```npm run dev``` and ```npm run prod``` produce different outputs (as expected). Minifying for production before pushing makes sense, but the problem is that git sees the minified ```app.js``` as different than the non-minified one. So, git always thinks ```app.js``` has changed. How do you handle this? – Andy White Dec 11 '20 at 19:05
  • @AndyWhite I am never pushing the version compiled by `npm run dev` into the repository, always the minified one. So while developing locally I always have `npm run watch` running, before I am ready to push I run `npm run prod` and that's it. – nakov Dec 11 '20 at 20:55
  • @nakov, that makes sense and I appreciate the follow up. With ```npm run watch```, it will compile for dev which will be different than the minified one that is in the repo. I guess it's a personal preference, but I don't like seeing 'changed' files in git that aren't really changed. My solution was to add an ```npm run watch-prod``` command which compiles for production. That removes the entire point of having a dev build, but I couldn't think of anything else. – Andy White Dec 13 '20 at 02:26