Search code examples
phplaravelcsvcommandnpm-run

Is it possible to create custom npm run commands for Laravel?


Laravel has the following built-in npm run commands (among others):

npm run install
npm run watch

Is it possible to create custom npm run commands to run custom PHP scripts? For example, I want to create a command called npm run csv that will run a PHP script that imports a bunch of CSV data into a database.

Thanks.


Edit: After asking the question and seeing a lot of the responses, it has become overwhelming obvious that writing a php artisan command is probably the better way to go. As such, that's what I will do.

Thank you all for your responses. As for why I didn't ask that question, it's quite simple: I didn't know that that was a better approach. I'm still new to Laravel and learning. Thanks.


Solution

  • At first, you must write Artisan Console Command. Then you could run it using npm. But this is not a recommended way. you can run any artisan command like:

    php artisan inspire
    

    If you want to run this with npm just add this command in the package.json's script. For example:

    {
       "scripts": {
           "inspire": "php artisan inspire"
       }
    }
    

    Then run the command like this:

    npm run inspire