Skip to content

Instantly share code, notes, and snippets.

@rosszurowski
Last active March 16, 2023 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosszurowski/eee05cebb67fd869b7db1914b844db9c to your computer and use it in GitHub Desktop.
Save rosszurowski/eee05cebb67fd869b7db1914b844db9c to your computer and use it in GitHub Desktop.
Modifying $PATH in Make on macOS
# You can, in fact reference node_modules without the prefix by prepending node_modules to your $PATH with this line:
export PATH := ./node_modules/.bin:$(PATH)
# But for some reason, on macOS, Make doesn’t recognize the $PATH when it’s the start of a command.
# So this rule doesn’t work. It treats `prettier` as a file/directory:
format: node_modules
@prettier --write .
# But this rule does:
format: node_modules
@true && prettier --write .
# Erring on the side of simplicity and clarity, it's probably best to just include the full prefix.
@nebbles
Copy link

nebbles commented Mar 12, 2023

On macOS, the path is correctly updated if you've set SHELL=bash at the top of the Makefile. I think this keeps the Makefile fairly portable still as most systems already have bash installed too.

Loved your post about Makefiles by the way

@rosszurowski
Copy link
Author

rosszurowski commented Mar 12, 2023

Thanks @nebbles! And I haven't yet found an incantation that works. When I run this Makefile:

export SHELL := /bin/bash
export PATH := ./node_modules/.bin:$(PATH)

format:
	@prettier --write .

I still get the following error:

$ make format
make: prettier: No such file or directory
make: *** [format] Error 1

I've also tried it with SHELL := /bin/bash, SHELL = /bin/bash, export SHELL = /bin/bash and adjusting its positioning relative to the export PATH call.

Are you running a non-stock version of Make perhaps? Or is there a different incantation to use?

@nebbles
Copy link

nebbles commented Mar 16, 2023

Oo interesting. This is the top of the Makefile I ran:

SHELL=bash
export REACT_APP_GIT_HASH := $(shell git rev-parse HEAD)
export PATH := ./node_modules/.bin:$(PATH)

I'm using stock make, but a bit older

GNU Make 3.81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment