NODE_PATH not recognized - node.js

Here is my package.json script:
"scripts": {
"start": "NODE_PATH=$NODE_PATH:./shared node",
"dev": "npm run start & webpack-dev-server --progress --color"
},
When I run npm start in Windows 8 it shows the below error:
node_path is not recognized as a internal or external command, operable program or batch file

I had the same problem when I wanted to set the environment variable in a browserify script:
"scripts": {
"build:symlinked": "NODE_PATH=./node_modules browserify src/index.js > dist/build.js"
}
To be able to use linked node modules that are requiring peer-dependencies.
As mentioned above, you can try to set the environment variable manually or by script where it seems you have to use different commands depending on what command line tool you use.
For not having to do this every time, I found that npm package: cross-env.
By installing it and applying the script like this
"scripts": {
"build:symlinked": "cross-env NODE_PATH=./node_modules browserify src/index.js > dist/build.js"
}
I was able to solve that problem. This is mainly useful, if you work in a team with mixed MAC/Linux and Windows users, so you don't have to to take care about applying the Environment variables in such scripts anymore.

You don't need to define environment variable in package.json just use this
{
"scripts" : "node server.js"
}
or define what you want, here is the reference link.

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

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?
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

Volta with yarn run build system cannot find the path specified

This is the first time I'm using Volta, so bear with me.
I have installed globally typescript, node and yarn
yarn -v >> 1.22.10
node -v >> v14.15.4
npm -v >> 6.14.10
These commands work inside and outside my project folder, with the same results.
However if I use yarn build from inside vscode the output is an error stating:
System cannot find the path specified
If I do the same from outside vscode I get the same result:
If I go to the node_modules/.bin folder inside vscode, the command still doesn't work (this time I just only run tsc). The error is the following:
The term tsc is not a cmdlet recognized command, function, script file or executable program. Verify if the name is written correctly or, if there is an access route, verify the route is correct and try again.
But if the command is executed from outside vscode in a cmd window, it works as expected, because tsc is really there:
Additionally, if I run npm run build inside vscode, the command works as expected. This is my package.json:
{
"name": "socket-server",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "yarn build --watch --preserveWatchOutput",
"lint": "eslint src --ext js,ts",
"test": "jest"
},
"devDependencies": {
"eslint": "^7.19.0",
"jest": "^26.6.3",
"typescript": "^4.1.3"
},
"volta": {
"node": "14.15.4",
"yarn": "1.22.10"
}
}
I do suspect of volta because volta is managing yarn version, but no npm; but I don't really know what's the issue.
I'm using Windows and my PATH has the following entries:
What am I doing wrong?
Edit: Yes, Shell command shortcut exists:
the problem is about vsCode, you should run code . in cmd because if you Open the Command Palette (Ctrl + Shift + P) and type
Shell Command: Install 'code' command in PATH
you won't see noting, after running code . in cmd when you should see like this photo, every things will be fine
I’m not sure for Windows, but usually the scripts in node_modules/.bin are symbolic links to scripts. For instance, node_modules/.bin/tsc might point to node_modules/typescript/bin/tsc.
It works outside of the directory because then it uses the global version of tsc.
Seeing your error, I’m suspecting that the symlinks are broken. Maybe just try to remove node_modules directory and redo an npm install.
PS: I’m not familiar with Volta, but it looks more like an NPM problem.

'NODE_OPTIONS' is not recognized as an internal or external command

I'm on a windows 10 machine trying to run a build script from the git bash terminal.
On my terminal node is recognized just fine, for example I get the version when I run node --version.
But running the build script fails with the following error:
'NODE_OPTIONS' is not recognized as an internal or external command,
operable program or batch file.
I'm guessing I need to add something to my PATH variables to get this to work, but what?
Use cross-env package which easily sets environment variables.
Step 1:
Install cross-env from npm
npm i cross-env
In your package.json file (In this example your need is to run 'start' command which has 'NODE_OPTIONS')
{
"name": "your-app",
"version": "0.0.0",
"scripts": {
...
"start": "NODE_OPTIONS=<your options> <commands>",
}
}
Step 2
Add 'cross-env' in the script which you need to run NODE_OPTIONS. (In this case 'start' script)
{
"name": "your-app",
"version": "0.0.0",
"scripts": {
...
"start": "cross-env NODE_OPTIONS=<your options> <commands>",
}
}
For me installing the below mentioned package solved the problem
npm install -g win-node-env
Not a PATH issue, NODE_OPTIONS is an ENVIRONMENT VARIABLE that needs to be set before starting your build. To set en environment variable in Windows 10 you need to use the set command in a terminal mode. See this article on SUPERUSER forum to learn more.
In your case, just add set before NODE_OPTIONS and that will fix your issue.
Here's how to integrate it in package.json:
...
"scripts": {
...
"build": "set NODE_OPTIONS=--max_old_space_size=4096 && next build"
...
}
...
A way to launch both the node process and the debugger via F5, which does not require wrestling with env vars.
Make sure .vscode/launch.json is deleted.
1. Open the Run & Debug pane
2. Click on Node.js
3. DO NOT click on "Run script: dev" directly, instead click on the cog next to it
4. Your launch.json should look similar to:
{
"configurations": [
{
"type": "node-terminal",
"name": "Run Script: dev",
"request": "launch",
"command": "yarn run dev",
"cwd": "${workspaceFolder}"
}
]
}

Nothing happens when entering the babel command

I just installed babel as followed: npm install -g babel-cli
However when I try to enter the babel command in either nodejs cmd (as admin) or my editor pycharm's terminal, nothing happens.. It's like it's loading or something but even waiting for more than 20min. nothing appeares.
Screenshot
This started happening after I added C:\Users\sebas\PycharmProjects\storage\node_modules\.bin as a path to the environment variables
Before that, every time I tried running babel I would get a babel command not recognized as an external or internal command error.
What is going on?
In my opinion, the easiest way to solve this (see: not adding to your path) is to simply add an entry inside your package.json
For example:
...
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src -d lib --copy-files",
},
...
This way, all you need to do is run npm run build, and it will take care of figuring out where babel is. Also, remember to add a .babelrc with the following content if you're using babel-preset-env:
{
"presets": ["env"]
}
More than likely because you have not added the .\node_modules\.bin directory to your path.
Until you do that, you can use .\node_modules\.bin\babel

Resources