Reputation: 93
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
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:
./node_modules/.bin/cross-env
npx cross-env
Upvotes: 4
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 usenpx
, it first checks if the required package is already installed locally in thenode_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 tonpx
.
Upvotes: 0