Search code examples
node.jsnpmlerna

Why can npm run babel-node from script but not as npm exec?


In a lerna monorepo, babel is installed only at the root package level. When running npm exec babel-node from root/packages/packageA, it fails with:

npm ERR! could not determine executable to run

From the same directory, running npm run test, where test is just set to babel-node opens the babel-cli.

I.e. why can npm resolve the path to the root node_modules' bin when run as a script but cannot when run from the terminal directly?

EDIT:

I.e. https://github.com/bishonen/newproject/tree/master/packages/someapp

when running npm run test-babel, it will work from any of the 3 directories which contain a package.json. When running npm exec babel-node, it will only work from the root directory.


Solution

  • Workspaces support for both npm run and npm exec would be supported in npm@7.7.0 or greater. See the details here

    You can use the feature updating npm

    npm i -g npm@7
    

    I try it and it works in your code https://github.com/bishonen/newproject/tree/master/packages/someapp

    I use npm v7.12.0