5

I have the following line in my package.json

"scripts": {
  "start": "cross-env NODE_ENV=development node index.js"
}

I can see that "yarn start" command is running fine, but when I run "cross-env NODE_ENV=development node index.js" command directly in the terminal, I am getting the following error:

zsh: command not found: cross-env

If cross-env is not registered in the terminal, how does "yarn start" command works?

yellowspectre
  • 51
  • 2
  • 4
  • Because you have it installed in node_modules. If you want to use it outside, you would need to do `npm install --global cross-env` to use it anywhere. Just because you're cd'd n the project does not mean you will be able to use commands inside `node_modules`. `./node_modules/.bin/cross-env` should also work. – DemiPixel Oct 07 '21 at 15:30
  • I get that, then why does "yarn start" command work? My understanding is "yarn start" just runs the "cross-env NODE_ENV=development node index.js" command in terminal. – yellowspectre Oct 08 '21 at 07:12

2 Answers2

2

https://docs.npmjs.com/cli/v7/configuring-npm/folders#executables

When in local mode, executables are linked into ./node_modules/.bin so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test.)

It's simply a feature to make things easier. It also means if you're working a project with multiple people, you only have to npm install --save a module--you don't have to worry about everyone in your project manually installing it globally. If you wish to run it yourself on the command line, you can either:

  • Install the module globally
  • Manually type in the command line ./node_modules/.bin/cross-env
DemiPixel
  • 1,768
  • 11
  • 19
0

So, I see that people have informed you that yarn pulls cross-env from the local project's node_modules directory.

In addition to installing cross-env globally (install --global cross-env) so that you can run cross-env NODE_ENV=development node index.js in your terminal, you can also use npx to run it:

npx cross-env NODE_ENV=development node index.js

How does npx work?
When you use npx, it first checks if the required package is already installed locally in the node_modules folder of your current project. If the package is not found, npx downloads the package from the npm registry and stores it temporarily in a cache folder specific to npx.

AlexMelw
  • 2,406
  • 26
  • 35