how does cross-env command works in nodejs? - node.js

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?

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

Related

'NODE_ENV' is not recognized as an internal or external command, [duplicate]

I'm trying to setup an environment for a Node.js app. but I'm getting this error every time.
"NODE_ENV" is not recognized as an internal or external command,
operable command or batch file.
What does this mean and how can I solve this problem?
I'm using Windows and also tried set NODE_ENV=development but had no luck.
I wrote a module for this: win-node-env.
It creates a NODE_ENV.cmd that sets the NODE_ENV environment variable and spawns a child process with the rest of the command and its args.
Just install it (globally), and run your npm script commands, it should automatically make them work.
npm install -g win-node-env
It sounds like your error comes from an attempt to run something like this (which works in Linux)
NODE_ENV=development node foo.js
the equivalent in Windows would be
SET NODE_ENV=development
node foo.js
running in the same command shell. You mentioned set NODE_ENV did not work, but wasn't clear how/when you executed it.
for windows use & in between command also. Like,
"scripts": {
"start": "SET NODE_ENV=development & nodemon app/app.js",
}
npm install --save-dev "cross-env" module.
modify the code as cross-env NODE_ENV=development node foo.js.
Then you can run the like npm run build.
Use win-node-env, For using it just run below command on your cmd or power shell or git bash:
npm install -g win-node-env
After it everything is like Linux.
I had the same problem and on windows platform and i just ran the below command
npm install -g win-node-env
and everything works normally
set NODE_ENV=production & nodemon app/app.js
will cause NODE_ENV to contain a space at the end:
process.env.NODE_ENV == 'production'; //false
process.env.NODE_ENV == 'production '; //true
As mentioned in a comment here, use this instead:
NODE_ENV=production&& nodemon app/app.js
Changing your scripts to accommodate Windows is a royal pain. Trying to figure out the appropriate Windows translations and maintaining 2 sets of scripts is no way to live your life.
It's much easier to configure npm to use bash on Windows and your scripts will run as is.
Simply run npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe". Make sure the path to the bash executable is correct for your machine. You'll likely need to start a new instance of the terminal for the change to take effect.
The screenshot below illustrates the benefit.
npm ERR! when trying to run script initially.
Script modified for Windows use runs but doesn't show the return message.
After updating npm config to use bash, the script runs and returns the appropriate message.
For those who uses Git Bash and having issues with npm run <script>,
Just set npm to use Git Bash to run scripts
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe" (change the path according to your installation)
And then npm will run scripts with Git Bash, so such usages like NODE_ENV= will work properly.
This worked for me since it's an easy fix. I cloned a repository which was developed in WINDOWS but I am using MACOS.
If you are using windows use SET as prefix:
"scripts": {
"dev": "SET NODE_ENV=development && nodemon index.js",
},
But if you are using MacOS remove the SET keyword and use :
"scripts": {
"dev": "NODE_ENV=development && nodemon index.js",
},
So in a nutshell
if you are using windows use SET prefix before your run scripts and remove SET from MacOS (probably LINUX also) as shown above.
Do this it will definitely work
"scripts": {
"start": "SET NODE_ENV=production && node server"
}
NODE_ENV=development & node [your file name here]
or
SET NODE_ENV=development & node [your file name here]
You can solve this if you're using "Yarn Packager" by the following command:
yarn global add win-node-env
npm install -S cross-env
Worked for me
For windows
open git bash and try
NODE_ENV=production node app.js
If anyone else came here like me trying to find a solution for the error:
'env' is not recognized as an internal or external command
The reason I got this is that I was migrating an angular solution from a mac development machine over to a windows 10 desktop. This is how I resolved it.
run npm install --save-dev cross-env
go into my package.json file and change all the script references from env <whatever> to cross-env <whatever>
Then my commands like: npm run start:some_random_environment_var now run fine on Windows 10.
For windows you can do it like
"scripts": {
"start:prod" : "SET NODE_ENV=production & nodemon app.js",
"start:dev" : "SET NODE_ENV=development & nodemon app.js"
},
Most of the answers up there didn't help me..
What helped me was NODE_ENV=production&& nodemon app/app.js
Take note of the space.
Good luck.
set the script "test" inside the "package.json" file :
FOR EXAMPLE:
In Windows;
"test": "SET NODE_ENV=test & jest",
In Linux/Mac;
"test": "NODE_ENV=test jest",
you can use this
"scripts": {
"start:dev": "nodemon server.js",
"start:prod": "SET NODE_ENV=production & nodemon
server.js"
},
or you can install this
npm install -g win-node-env
and you can run NODE_ENV without SET
"start:prod": "NODE_ENV=production nodemon server.js"
"set NODE_ENV=production&& nodemon server.js" this one works for me.
set NODE_ENV=**production&** nodemon server.js
& must be joined because if you put space between production and & then
NODE_ENV will contain space in last like this 'production '
So just remove space between production and & and add space after &
process.env.NODE_ENV is adding a white space do this
process.env.NODE_ENV.trim() == 'production'
below code for windows
"start": "SET NODE_ENV=development & nodemon app.js",
"prod": "SET NODE_ENV=production & node app.js"
You can use this syntax (using "cross-env") ->
cross-env NODE_ENV=prod node dist/main
On a windows platform
($env:NODE_ENV="environmentName") -and (node file.js)
Kill the terminal( Ctrl + C) then run the file
node file.js
If you are using Powershell as your terminal by any chance, try Using Git-Bash for the same.
NODE_ENV=development node foo.js
try using this
NODE_ENV =development node server.js

webpack command not recognized

I have the below commands configured in npm scripts.Now, if i run the command "npm run dev" there is no response in command prompt. However, if the run the below command as "webpack -wd" in command prompt, then the command is working as expected. why, i am not able to run this command from npm scripts. I am using windows and have set the node path in environment variables as below
"scripts": {
"dev": "webpack -wd",
"test": "echo \"Error: no test specified\" && exit 1"
}
C:\Program Files\nodejs;C:\Program Files\nodejs\node_modules\npm\bin;C:\Users\[username]\AppData\Roaming\npm
If you want to run webpack via the command line, you either need to install webpack-cli globally with npm i -g webpack-cli or you need to use npx like npx webpack -wd. This will run webpack the same way npm scripts do.
The problem you are trying to circumvent is that webpack by default does not build the first time in watch mode, so you either need to change a file after starting webpack or run webpack without watch.
Note: npx is part of the NPM/Node.js bundle

can i install a package locally and globally at same time in node.js?

I am working on a project. I have worked with nodemon that is installed globally for development.
I edited my package.json file to add a script to automatically run a nodemon script - as shown below:
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon -e js,pug"
}
Now when another developer runs:
npm start dev
they will surely get a error if they have not installed the nodemon module.
I know that the solution is to install nodemon locally as a development dependency.
Is it possible to work around this problem without installing it locally?
Can I install nodemon both locally and globally at the same time?
simply install it globally and you can use it in any of your project
command :
npm i -g nodemon
now you don't need to install it locally at all to make it work on your project.

How to run few npm scripts in one-line shell command?

I have three scripts in package.json:
Watch server TypeScript
Nodemon
Webpack
"scripts": {
"watch-server": "tsc --watch --project ./server/tsconfig.json",
"watch-node": "nodemon --watch ./server/build/ --watch ./server/templates -e js,json,pug",
"watch-client": "webpack --config ./webpack/webpack.dev.conf.js --watch"
}
Everytime I start my computer and open VS Code I need to open three separate PowerShell terminals and type in those commands one-by-one. Is there any way to launch these three separate terminals with their own commands in one shell command? Maybe via tasks.json?
On linux or any bash terminal, you can use && to combine multiple commands, i
You can do as
npm run watch-server && npm run watch-node && npm run watch-client
A quick google search for powershell suggested using semicolon
so on powershell you can do something like below if using && does not work
npm run watch-server;npm run watch-node ; npm run watch-client
Also keep in mind, you can additionally add fourth command in your npm scripts in package.json where you can use one of these combined commands which works for you, like
start-all: npm run watch-server && npm run watch-node && npm run watch-client
and then run
npm run start-all

Run npm scripts using local deps

Currently I run npm scripts using local deps this way:
package.json:
"scripts": {
"test": "node ./node_modules/karma/bin/karma start",
"node-test": "node ./node_modules/jasmine/bin/jasmine",
"build": "node ./node_modules/gulp/bin/gulp build"
},
I don't want to use global deps, since I can forgot to add deps to the package.json. This way when a local dep is missing, then I got an error message and I don't have problems because some deps are not installed globally, e.g. karma plugins.
Is there a better (shorter) way to define npm scripts using the local libs? Is this travis compatible?
edit:
If it wasn't obvious I have the same libs installed globally, but I want to use the local installs by these projects. That means when I start karma with karma start then the globally installed version will start the karma server, which means that if I don't have all of the karma plugins globally installed, then I got error.
Another problem that I have windows, so the solutions described here: How to use package installed locally in node_modules? do not work. Windows does not recognize the #!/bin/sh and the #!/usr/bin/env node head sections and there is no sh command as far as I can tell. At least not in webstorm terminal. Git bash has the sh command, but I want to run these npm scripts from webstorm terminal.
One possible solution could be to fix somehow webstorm so it could use sh from terminal. After that I could use $(npm bin) I assume. But that's just a guess. I am not sure whether this can be done.
npm automatically puts prepends the path ./node_modules/.bin to your PATH env before it executes commands run by using npm run (including the two "magic" shortcuts npm start and npm test)
npm scripts docs
You can just set this up with:
"scripts": {
"test": "karma start",
"node-test": "jasmine",
"build": "gulp build"
}
Assuming that you have karma, jasmine and gulp-cli listed in either your devDependencies or dependencies (so that they're install when doing npm install)
And yes, it is travis-compatible. Here is an example of a package that is tested on travis using tap which is installed locally as a module:
https://github.com/scriptoLLC/couchdown/

Resources