Max
Max

Reputation: 934

bash not running npm or npx commands

I am currently trying to programmatically run a npx eleventy function through a bash script (on a cronjob)

I have tried it on my terminal, it works perfectly fine, ran a command as such

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx eleventy

Also did checked the following

npm -v - 6.14.5 and

npx -v - 6.14.5

running which npm returns /usr/local/bin/npm and running which npx returns /usr/local/bin/npx

However, running the following commands (below) in the bash script doesn't work...

test.bash

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx eleventy
cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx -v
cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npm -v

throwing the following error: env: node: No such file or directory.

However, it seems that I do have node on running the following command,

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/node -v

returns v12.16.1

Seek some help please!

Edit:

my cronjob

* * * * * bash /Users/me/Desktop/test.bash >> /Users/me/Desktop/testvimbackup.log 2>&1

Output of echo PATH=$PATH 1>&2:

PATH=/usr/bin:/bin

Upvotes: 2

Views: 2214

Answers (1)

Tibebes. M
Tibebes. M

Reputation: 7538

I think the issue is because crontab run with an empty environment.

try sourcing your profile like this (in the script)

. $HOME/.bash_profile

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx eleventy
cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx -v
cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npm -v

(or try with . $HOME/.profile or $HOME/.bashrc if it didn't work)

Alternative: setting the path to node manually

export PATH=$PATH:/usr/local/bin


cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx eleventy

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npx -v

cd /Users/me/Desktop/test-folder/public && /usr/local/bin/npm -v

Upvotes: 1

Related Questions