'source: not found' in the context of npm scripts
Posted on
Context
Today at work I found out that the following script from the scripts section
of our package.json was not working on my colleague's machine. He is running
Ubuntu. I have seen this happening also on the Arch distribution.
"start:dev": "source .env; nodemon index.js"
This script aims at sourcing a bunch of environement variables and then only running the application.
This had worked under OSX since the beginning of the project though.
What is happening
Basically, if not specified as a npm config, npm-run uses the system's
default /bin/sh as shell to execute commands in script entries.
This means that if your system is using a default shell that does not implement
the source command, you are getting this source: not found error.
But npm gets you covered 😅
Solution
The solution is to specify the npm's script-shell config, before running the
source command.
The script entry presented in the context section above would become:
"start:dev": "npm config set script-shell \"/bin/bash\"; source .env; nodemon index.js"
It is a bit wordy but does usually get you out of trouble as long as the system
running the command implements bash.