Install a Laravel project’s Node.js dependencies

(This is part of a larger guide to deploying a Laravel and Vue.js web application.)

(You can skip this article if your Laravel app has no JavaScript dependencies.)

This short article shows how to install the Node.js JavaScript runtime environment and NPM, the standard package manager for the Node.js. We’ll use NPM to install your application’s Node.js dependencies.

(This is trivial if you’ve done it before, and it might be overkill to make a dedicated article for this. But I want to document every step so everyone can follow along.)

Install Node.js

# Install Node.js and NPM
laravel@server$ sudo apt install nodejs npm

Install Node.js packages

Then change into the directory from which your app is served and use NPM to install your app’s Node.js dependencies:

# Install your app's Node.js dependencies
# (They should be installed in your project's `node_modules` directory.)
laravel@server$ cd /srv/www/laravel-project
laravel@server:laravel-project$ npm install

This command looks in your Laravel project’s package.json file and installs the project’s Node.js dependencies into a node_modules directory in your project’s root.

Warning: problems with outdated Node.js on Ubuntu LTS and Debian stable

The version of Node.js shipped with Ubuntu LTS and stable Debian (which I imagine many readers are using) tends to be quite outdated. This could cause problems when running npm install if your app’s package.json requires a recent version of Node—in this case NPM will warn you that your Node.js is outdated.

You can solve this by first uninstalling your outdated version of Node.js (e.g. using sudo apt purge nodejs && sudo apt autoremove), then following the instructions for installing an up-to-date version of Node.js on the Nodesource GitHub page. At the time of writing, I recommend installing Node version 20 or later.

Fix vulnerabilities in Node.js dependencies (optional)

The npm install command may have warned you about vulnerabilities in installed packages. In this case you should run npm audit fix, which will try to (and generally succeed in) fixing these vulnerabilities:

# If necessary, try to fix vulnerabilities in Node.js packages
laravel@server:laravel-project$ npm audit fix

Build your app

After running npm install (and npm audit fix, if needed), you can now build your app for production (this assumes you have a build script defined in your project’s package.json file, which a Laravel-Vue or Laravel-React project should come with by default):

# Build your app for production.
# (Assets should be outputted to your project's `public/build/` directory)
laravel@server:laravel-project$ npm run build
Warning: npm run build can fail because of insufficient RAM

The npm run build command can unexpectedly fail (it will exit with the message Killed, and fail to produce a public/build directory) if your machine has too little RAM, which might happen on lightweight servers.

The solution is the same as when installing Composer—create a swap file with more RAM (1 GB should be plenty). See the Composer article for details.

Remove public/hot, if necessary

One final detail: double check that there is no public/hot directory in your server-side Laravel project; delete it if necessary.

(This directory is used by Vite for hot reloading during development, but will cause problems on a production machine. It shouldn’t end up on your server because it is ignored in Laravel’s default .gitignore, but check just in case.)

Next: The next article covers the necessary environment configuration for running a Laravel app in production.

Finding this tutorial series useful? Consider saying thank you!

The original writing and media in this series is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.