Ionic Build Command Not Found

I’m trying to do a simple Ionic build but cannot find a way to make it work. Any ideas greatly appreciated.

“sudo npm install -g ionic” is successful and outputs:

#!/bin/bash -eo pipefail
sudo npm install -g ionic
/usr/local/bin/ionic -> /usr/local/lib/node_modules/ionic/bin/ionic
+ ionic@4.12.0
added 271 packages in 9.704s

When “ionic build” runs it errors with “command not found”. I’ve tried adding a path but non of the following have worked (no such file or directory):

./node_modules/.bin/ionic build
./node_modules/ionic/bin/ionic build
/usr/local/lib/node_modules/ionic/bin/ionic

My simplified config.yaml:

version: 2.1

jobs:
  build:
	working_directory: ~/tmp
	docker:
	  - image: circleci/node:9.9
	steps:
	  - checkout
	  - run:
		  name: Install Ionic 4.
		  command: sudo npm install -g ionic
	  - run:
		  name: Install dependencies.
		  command: npm install
  deploy-development:
	working_directory: ~/tmp
	machine:
	  enabled: true
	steps:
	  - run:
		  name: Build app using development environment variables.
		  command: ionic build --configuration development
workflows:
  version: 2
  build-and-deploy:
	jobs:
	  - build
	  - deploy-development:
		  requires:
			- build
		  filters:
			branches:
			  only: develop

The core problem here appears to be jobs don’t share the same stuff. So the “build” job does global npm installs but the “deploy-development” doesn’t have access to these. Difficult to tell but it seems that caching global npm modules is needed here. This post has an example but hard coding paths seems very brittle: https://stackoverflow.com/questions/31766930/circleci-not-caching-my-globally-installed-node-module

I was able to get this to work by combining both the “build” and the “deploy-development” jobs but that’s not ultimately what we need. In our case, we want a branch to always trigger a build/test but only specific branches to trigger a deploy.