locally installed grunt, “grunt exec:blank” fails with “/bin/sh: grunt: command not found”

magento.stackexchange https://magento.stackexchange.com/questions/312718

  •  13-04-2021
  •  | 
  •  

Question

I am hosted at Nexcess and tried to implement Grunt to assist with LESS development. I followed the Magento 2 docs for installing grunt. NodeJS 10.10.0 is installed.

The first deviation from the instructions has to do with how grunt, grunt-cli, and lessc were installed. The instructions say to install globally, with the -g parameter, but as this is shared hosting, that's not possible.

So instead I install locally to ~/public_html/. This creates a node_modules folder. This was followed up with npm install and npm update. I can now run grunt with its full path from the magento public_html folder, but I get an error:

~/public_html/node_modules/grunt-cli/bin/grunt exec:blank

 [public_html]$ ./node_modules/grunt-cli/bin/grunt exec:blank
Running "exec:blank" (exec) task
  /bin/sh: grunt: command not found
  Exited with code: 127.
  Error executing child process: Error: Process exited with code 127.
Warning: Task "exec:blank" failed. Use --force to continue.

Aborted due to warnings.

Running the clean: or less: commands both work on the blank theme. Only exec fails.. That is, I should say.. clean:blank works while less:blank results in some additional messages that I am not sure how to explain:

 public_html]$ ./node_modules/grunt-cli/bin/grunt less:ms
Running "less:ms" (less) task
>> Destination pub/static/frontend/MS/theme-frontend-ms/en_US/css/styles-m.css not written because no source files were found.
>> Destination pub/static/frontend/MS/theme-frontend-ms/en_US/css/styles-l.css not written because no source files were found.
>> Destination pub/static/frontend/MS/theme-frontend-ms/en_US/css/email.css not written because no source files were found.
>> Destination pub/static/frontend/MS/theme-frontend-ms/en_US/css/email-inline.css not written because no source files were found.
Was it helpful?

Solution

The grunt exec command expects grunt to be in $PATH. Since the node_modules bin path isn't commonly included in the $PATH variable, that would explain this issue. There's more than one way to solve this problem:

  • Install grunt-cli globally: https://gruntjs.com/getting-started#installing-the-cli
  • Make the locally-installed grunt-cli accessible in your path: PATH=./node_modules/.bin:$PATH. This can be done multiple ways:
    • on a per-command basis: PATH=... ./node_modules/.bin/grunt <cmd>
    • in a .<shell>rc file: export PATH=./node_modules/.bin:$PATH
    • in /etc/profile just the same way as the previous option
    • n.b. the two previous options will require you to update your current shell with the config file or start a new one
  • Use direnv to update your path only where necessary. The .envrc file can be added to your current project root directory, and direnv allow will update your current shell based on the contents of .envrc

Have fun!

P.S. here's where Magento generates the command to run grunt as a child process: https://github.com/magento/magento2/blob/2.3.5/dev/tools/grunt/configs/combo.js#L17. Note that it doesn't specify a path to grunt, just assumes invoking grunt will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top