yellowspectre
yellowspectre

Reputation: 93

how does cross-env command works in nodejs?

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?

Upvotes: 9

Views: 25622

Answers (2)

DemiPixel
DemiPixel

Reputation: 1881

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
  • Install npx and use npx cross-env

Upvotes: 4

AlexMelw
AlexMelw

Reputation: 2634

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 (npm 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.

Upvotes: 0

Related Questions