Fixed - NPM ERR! could not determine executable to run

Issues with NPX? Fixes for npm ERR! could not determine executable to run

Feb 17, 2023 | Read time 11 minutes

🔔 Table of contents

Introduction

When working with NPM or NPX projects you sometimes come across this error:

npm ERR! could not determine executable to run

The more complete error could look something like this below:

npm ERR! could not determine executable to run

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\myuser\AppData\Local\npm-cache\_logs\2023-02-19T04_34_26_594Z-debug-0.log

This is common when you are spinning up a NUXT project or even a testing project using Cypress and it requires you to run NPX

(In my case, I came across this when I was playing around with the Husky NPM package)

The main reason you are getting “npm ERR! could not determine executable to run” is because while using npx - it could not determine the executable script.

What is NPX anyway?

npx is a command-line tool that is bundled with the Node.js runtime and was introduced in version 5.2.0. It allows developers to execute Node.js packages without installing them locally.

npx runs a specified command by first checking whether it exists in the local node_modules directory. If it does not exist, it will download and install the package from the > npm registry temporarily, execute the command, and then remove the package. This means that you don’t have to worry about installing a package globally or managing # >dependencies when using a package just for a single command or script.

For example, instead of installing the mocha test framework globally with:

npm install -g mocha,

you can run it using npx mocha and it will automatically download and install the latest version of mocha temporarily.

To understand this error, we need to understand how NPX behaves. NPX will look for executables in your $PATH, node_modules/.bin folder, global cache and then git repos. If it cant find the executable with the exact name, then it will throw ...could not determine executable to run

To fix the “could not determine executable to run” error, we can try the following steps:

  1. Make sure that NPM and NPX is installed correctly
  2. Check that you are using the NPX command correctly
  3. Verify that the executable exists
  4. Clean out your Git Hooks (.git/hooks) folder if you are using packages like Husky
  5. Clean your project folder - node_modules, package-lock.json and install again

1. Make sure that NPM and NPX is installed correctly

First thing to fix these issues to to make sure that we have installed NPM and NPX correctly.

To verify that the latest version of npm has been installed, run the following command:

npm -v

This will display the version of npm that is currently installed on your system.

If it is not the latest version, continue on!

  1. Open the terminal and do the following steps to install NPM:
  2. Run the following command to update your version of npm to the latest version:
npm install -g npm@latest

This command will install the latest version of npm globally on your system.

Now to make sure you have NPX, open the terminal and run:

npx -v

If you don’t even have NPX installed, then we can install it with the command:

npm install -g npx

This will install it globally for your machine. Make sure to restart the terminal to start using NPX!

2. Check that you are using the NPX command correctly

A common reason why this error comes up is that we are not using the npx command correctly.

NPX follows the format:

npx [options] <command>[@version] [command-arg]...

As an example, lets say we want to call the cowsay package:

npx cowsay wow

In this example, cowsay is our command and wow is the command-arg

 __________________
< wow >
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Tips:

  • Double check that you are not misspelling the command name
  • Check that you are using it in the correct order - for example:

npx open cypress will not work. But:

npx cypress open will work!

The first example does not work because it is looking for the command open with a parameter cypress! Obviously this is not how to call the Cypress testing library.

3. Verify that the executable exists

The way NPX works is that it looks in a few places to run the executable. It will look in locations:

  • node_modules/.bin
  • check whether <command> exists in your $PATH directory
  • in the local project binaries

As an example, in NUXT projects they have provided a starter boilerplate project here:

https://github.com/nuxt/create-nuxt-app/blob/master/packages/create-nuxt-app/

Now if we look at the package.json file, we can see that there is a "bin": "lib/cli.js",. This tells NPX that there is a executable located in the lib folder called cli.js:

{
  "name": "create-nuxt-app",
  "version": "5.0.0",
  "description": "Create a Nuxt.js App in seconds.",
  "homepage": "https://github.com/nuxt/create-nuxt-app#readme",
  "repository": {
    "url": "https://github.com/nuxt/create-nuxt-app.git",
    "type": "git"
  },
  "bin": "lib/cli.js",
  "files": [
    "lib"
  ],
  "dependencies": {
    "cac": "^6.7.14",
    "chalk": "^4.1.2",
    "cna-template": "^5.0.0",
    "cross-spawn": "^7.0.3",
    "envinfo": "^7.8.1",
    "lodash": "^4.17.21",
    "sao": "^1.7.1",
    "validate-npm-package-name": "^4.0.0"
  },
  "authors": [
    "Egoist <0x142857@gmail.com>",
    "Nuxt team <team@nuxtjs.org>",
    "Clark Du <clark.duxin@gmail.com>"
  ],
  "license": "MIT",
  "engines": {
    "node": ">=v14.20.1"
  }
}

So when we run the command:

npx create-nuxt-app <my-project>

It will go off and executes the cli.js file and creates a new NUXT project for you (https://github.com/nuxt/create-nuxt-app/blob/master/packages/create-nuxt-app/lib/cli.js ).

Now if there is no executable defined in that project, then you will see the error:

npm ERR! could not determine executable to run

4. Clean out your Git Hooks (.git/hooks) folder if you are using packages like Husky

If you are using a NPM package that is working with git hooks (such as Husky), then try using:

rm -rf .git/hooks
npm install

The above command is relating to a bug that was recent with Husky when upgrading from version 4-> 8.

(Note an additional command of npm rebuild might be needed)

What are Git Hooks?

Git hooks are scripts that Git executes automatically before or after certain Git events, such as committing changes, pushing changes, or merging branches. These hooks can be used to automate or customize your Git workflow.

Git hooks are stored in the .git/hooks/ directory of your Git repository, and they are not typically tracked in the repository itself. By default, Git provides a number of sample hooks that you can customize for your own purposes.

Git hooks can be a powerful tool for automating your Git workflow and ensuring that your team adheres to certain guidelines or standards. However, it’s important to remember that Git hooks are executed locally and are not enforced on remote repositories, so they should not be relied upon as a sole means of enforcing policies or standards.

Now writing Git Hooks can be tedious, so comes along Husky which got pre-made hooks for you to use. As an example, lets say we want to format and clean up our CSS, HTML files before each commit - we can use the lint-staged package with Husky.

Steps to get started include:

  1. Open the terminal and navigate to your project
  2. Run npm i husky lint-staged -D to install it as a dev dependency
  3. Update the package.json file to make sure we have the husky and lint-staged attributes:
{
  "name": "example-husky",
  ...
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,json,css,md}": [
      "prettier --single-quote --write",
      "git add"
    ]
  },
  "dependencies": {},
  ...
}

Now everytime we run git commit, the git Hook from husky will kick in and run the linter on our checked in files - making sure its formatted and no whitespaces!

$ git commit

✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ packages/frontend/.lintstagedrc.json — 1 file
    ↓ *.js — no files [SKIPPED]
    ❯ *.{json,md}1 file
      ⠹ prettier --write
  ↓ packages/backend/.lintstagedrc.json — 2 files
    ❯ *.js — 2 files
      ⠼ eslint --fix
    ↓ *.{json,md} — no files [SKIPPED]
◼ Applying modifications from tasks...
◼ Cleaning up temporary files...

Warning: Make sure to backup your .git/hooks folder

The above method will remove all of your .git/hooks scripts so make sure that you have backed up that folder. This is required because usually you do not check in git hooks and it works only for your custom setup.

5. Clean your project folder - node_modules, package-lock.json and install again

Now if the above did not work, we can go the full nuke method and clear everything out:

  1. Delete node_modules (or move it somewhere outside from the project directory)
  2. Delete the package-lock.json file
  3. Run rm -rf .git/hooks
  4. Upgrade to the latest version of npm npm install -g npm@latest
  5. Run npm install

Summary

In this post, I went over the common error of npm ERR! could not determine executable to run. This error is caused by using NPX and it not finding your executable. You will need to make sure that you are using the latest version of NPX, the executable exists and you have not mispelt it.

If you are using packages that works with Git Hooks, make sure to backup and clean out the .git/hooks folder with rm -rf .git/hooks

Lastly if the fixes above did not work, then do the nuke approach and remove node_modules, package-lock.json and reinstall with npm install!

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube