Search code examples
phpnode.jslaravel

I have Laravel project with node_modules folder. How can I deploy this project on cpanel. Please guide step by step for this


I have Laravel project with node_modules folder. How can I deploy this project on cpanel. Please guide me.

Current Laravel folder structure:
app
resources
bootstrap
....
....
node_modules
.env

I am beginner to Laravel. Before, I have deployed Laravel projects without node_modules. But now I have node_modules folder in project. Guide me to deploy. Is there need make build like 'Angular' or what. Please help.


Solution

  • Deploying a Laravel project that includes a node_modules folder generally indicates that your project is using Node.js dependencies, probably for front-end assets managed by tools like Laravel Mix. Here's a basic guide to deploy your Laravel project with node_modules:

    Step 1: Local Development

    Before deploying, you should ensure that everything works on your local machine. Typically, you would run:

    npm install
    npm run prod
    

    This installs all Node.js dependencies and compiles your assets using Laravel Mix (or another build tool specified in your package.json). The npm run prod command typically runs mix in production mode, which minimizes and optimizes your assets.

    Step 2: Prepare Your Laravel Project

    Ensure your Laravel project is ready for production:

    Environment Configuration: Set your .env file for production settings, especially the database connection, app environment (APP_ENV=production), and key (APP_KEY).

    Optimize: Run these Laravel commands:

    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    

    These commands optimize the configuration loading, routes, and views respectively.

    Step 3: Exclude node_modules from Deployment

    You generally do not want to deploy the node_modules folder to your production server. This folder can be massive and is unnecessary on the server if you're pre-compiling assets locally. Make sure your deployment process excludes this folder. If you're using a .gitignore file (in case of Git-based deployment), ensure that node_modules is listed in it.

    Step 4: Deploy Your Code

    Upload your code to the server. This can be done through various methods:

    Git: If your server has Git installed, you can clone the repository and then pull changes each time you deploy.

    FTP/SFTP: Use a tool like FileZilla to manually upload your files to the server.

    Step 5: Post-Deployment Setup

    Once your code is on the server, run the following commands from your project root on the server:

    1. Install Composer Dependencies:

    composer install --optimize-autoloader --no-dev
    

    2. Migrate Database:

    php artisan migrate --force
    

    3. Optimize Again (if needed):

    php artisan optimize
    

    Step 6: Handling Static Assets If you pre-compiled your assets (CSS/JS) using Laravel Mix or another tool:

    • Ensure these compiled assets (usually in the public directory) are uploaded to your server. They should be referenced correctly in your views.

    By following these steps, you should be able to deploy your Laravel project with front-end dependencies managed by Node.js efficiently and securely.