Error: Cannot find module 'sax'

Today ionic CLI prompted me with this message:

The Ionic CLI (local version) has an update available (3.9.2 => 3.10.3)! Would you like to install it?

Well, I did. Now I wish I didn’t!

After installing new version of CLI I got this error message:

Error: Cannot find module ‘/MyApp/node_modules/@ionic/app-scripts’

And a note:

Note: You can downgrade to your old version by running: npm install --save --save-exact ionic@3.9.2

I didn’t have time to deal with app-scripts problem, so I downgraded CLI to 3.9.2. After that I’m getting this message all the time, even after installing 3.10.3 again:

Error: Cannot find module ‘sax’

Really annoying!

Any ideas?

hit this command in command prompt “npm install sax”.

1 Like

Post your ionic info output please.
Also run npm list -g --depth=0.

cli packages: (/MyApp/node_modules)

@ionic/cli-utils  : 1.9.2
ionic (Ionic CLI) : 3.9.2

global packages:

Cordova CLI : 7.0.1 

local packages:

@ionic/app-scripts : 2.1.4
Cordova Platforms  : android 6.2.3 ios 4.4.0
Ionic Framework    : ionic-angular 3.6.1

System:

ios-deploy : 1.9.1 
Node       : v8.1.3
npm        : 5.3.0 
OS         : macOS Sierra
Xcode      : Xcode 8.3.3 Build version 8E3004b 

For some reason ionic info shows that CLI is 3.9.2 even though I installed the latest one (3.10.3)

npm install -g ionic@latest
/usr/local/bin/ionic → /usr/local/lib/node_modules/ionic/bin/ionic

  • ionic@3.10.3
    updated 1 package in 6.716s

The output of npm list -g --depth=0

/usr/local/lib
├── cordova@7.0.1
├── ionic@3.10.3
├── ios-deploy@1.9.1
├── npm@5.3.0
└── typescript@2.4.2

There are two Ionic CLI installs - a global and a local one. Run npm list --depth=0 in your project to see the local one, npm install ionic@latest to install the latest locally in the project.

Didn’t help. Still getting this error:

Error: Cannot find module ‘sax’
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object. (/MyApp/node_modules/elementtree/lib/parsers/sax.js:3:11)
at Module._compile (module.js:569:30)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)

Here is my ionic info after installing latest local cli:

cli packages: (/MyApp/node_modules)

@ionic/cli-utils  : 1.10.2
ionic (Ionic CLI) : 3.10.3

global packages:

Cordova CLI : 7.0.1 

local packages:

@ionic/app-scripts : 2.1.4
Cordova Platforms  : android 6.2.3 ios 4.4.0
Ionic Framework    : ionic-angular 3.6.1

System:

ios-deploy : 1.9.1 
Node       : v8.1.3
npm        : 5.4.2 
OS         : macOS Sierra
Xcode      : Xcode 8.3.3 Build version 8E3004b

Create a new project with ionic start and see if you can run this.

This one seems to be working.

Great, then compare package.json files from both.
If you are done, delete node_modules and package-lock.json from your project and run npm install.

What am I looking for in package.json?

For example ionic-native in my project is 3.13.1 and in new test project it’s 3.12.1. Does it matter?

I didn’t change anything in package.json, but removing node_modules and running npm install solved the problem.

Thanks @Sujan12!

Side question: I’m totally confused now with local and global versions. Wha’t the right setup?

Both on the newest version.

Why both are required?

I guess the CLI team didn’t feel confident that they could guarantee maintaining backwards compatibility, so decided to pin a specific version for each project.

Because people asked to be able to use one local version in one project, and another in another project.