Find where command is executed from in NPM

It can get confusing if you have packages installed globally, and within project folder. Find out where the command originates from when you are using NPM.

See below depending on what you are looking for.

Info about a command that you put in the command line

For e.g you do a -

vue create test1

.. and you see NPM run and create folder structure. But, you don’t know which this vue thing.

If you are just looking for a command used from the command line - it is more of the operating system function rather than NPM.

In Windows, do -

where <command>

In Linux -

where <command>

For example -

where vue

Output -

d:\dev\test1\vue
C:\Users\prashanth\AppData\Roaming\npm\vue
C:\Users\prashanth\AppData\Roaming\npm\vue.cmd
C:\Users\prash\AppData\Local\Yarn\bin\vue
C:\Users\prash\AppData\Local\Yarn\bin\vue.cmd

I have vue in the above folders on my computer.

Info about command from npm run

If you are looking for the origin of the command used in npm run <blah>, the easiest way to find out more is to look up the package/app documentation.

You could also do it the hard way. npm run <command> will start looking for the command in nearest node_modules\.bin directory, and start spanning out to the parent folders until it resolves or fails.

For e.g. -

  • you have installed axios in t1 folder
  • you create folder t11 under t1
  • you create folder t111 under t11

If you do an install or run, npm will go the parent, its parent and its parent until it finds t1, resolves the command from node_modules and runs it.

You can see which node_modules is used by doing -

npm root

`-- F:\dev\t1\node_modules

.. or,

npm bin

`-- F:\dev\t1\node_modules\.bin

The said script used in npm run should be in the .bin.

You can also use the method mentioned in the previous heading to find out where the command file is stored. If the operating system cannot find the command you will see the below message -

INFO: Could not find files for the given pattern(s).

Info about a particular package version

If you are looking for more of the version of the package used by your application and just confused about the version listed in package.json.

You can do a simple -

npm list axios

`-- 0.18.0
comments powered by Disqus