Using local npm modules with Make

I’ve written about the advantages of using npm scripts as a build system. Another alternative is to use Make, which works great for tasks with a lot of steps or if you prefer to keep the line length in check.

You can modify your PATH by adding the following at the top of your Makefile:

PATH := node_modules/.bin:$(PATH)

You can now use the binaries that same way as with npm scripts:

.PHONY: js

js:
	@browserify -t babelify -g uglifyify -e src/js/index.js > dist/index.js

You can also use some of the Make’s built-in variables and functions to make it more reusable:

SOURCE := src/js/index.js
TARGET := dist/index.js
FLAGS  := -t babelify -g uglifyify

.PHONY: js

$(TARGET): $(SOURCE)
	@mkdir -p $(dir $@)
	@browserify $(FLAGS) -e $< > $@

js: $(TARGET)

The $@ variable represents the target file while the $< is the source file.

Windows support

Make is available through Make for Windows, Gow and a dozen other installers.

comments powered by Disqus