NodeJS Error: node.js:810 var cwd = process.cwd(); - node.js

I'm new to nodeJS and also new to StackOverflow...
I'm starting to develop my first SPA, using RequireJS to compile my sources into a "dist" folder. I had NodeJS running a basic script to run my server:
var connect = require('connect');
connect.createServer(
connect.static(__dirname)
).listen(8080);
Everything was working well, till I compile my src again. That replaced all the files served by my server, so I though I would restart Node. I Ctrl^C and from this moment, I can't get Node to start again. When I try to run:
olivier$ node server.js
I get this error:
node.js:810
var cwd = process.cwd();
^
Error: ENOENT, no such file or directory
at Function.startup.resolveArgv0 (node.js:810:23)
at startup (node.js:58:13)
at node.js:901:3
What's strange is that I get the same error just trying to start NodeJS, simply doing:
olivier$node
Anyone has an idea of what I can do beside uninstalling Node and reinstalling it ?

I got this when trying to run the Node REPL from a directory I had already deleted (from another shell). Don't let this happen to you or you will be ashamed.

Could it be that RequireJS is also recreating the directory that contains your server.js?
Try and see if this works:
$ cd $PWD; node server.js
Although it seems useless to change the directory to the current directory, the rationale is that when a directory gets deleted while it's the current working directory of your shell, the shell is left in a dangling state because it's still 'attached' to the previously deleted directory. This also affects any processes that you start from that shell (like Node), and can yield confusing errors.
By executing cd $PWD, you make sure your shell gets 'reattached' to the newly created version of the directory, solving the dangling state.

use
cd .
is ok.
But the problem is in server

Remove the folder in another terminal and save its the file,
Open the new terminal on folder structure, and run the file.
Folder structure was not available on the current terminal session. When you close it and re-open the terminal in your file directory, the structure will be refreshed.

I faced the same issue.
To Solve this issue enter below command :
cd ..
OR
Check your directory in Terminal/CMD. and Change it.
Actually this issue coming when you deleted your project folder form your PC and without change directory try to run any command of React-Native.

I faced the same kind of a problem when starting the app in cluster mode via pm2.
I did the following & it worked,
* executed pm2 kill
* Removed node_modules
* npm install
* start the application
Reference: https://github.com/Unitech/pm2/issues/1244
Then it started to work as expected. Hope it helps.

If we try to do any actions on an already deleted file from another shell then we'll face this issue.
Just restart the system it'll work normally ( It worked for me )

I would suggest to close all the terminals and run below commands
sudo npm cache clean -f
sudo npm install -g n
and voila: no more process.cwd problems

Related

Node & Npm not recognized by WebStorm's terminal

I have paths to Node and NPM in both my user defined environment variable PATH and in system variable Path.
For node: C:\Program Files\nodejs
For npm: C:\Users\Username\AppData\Roaming\npm
Now node and npm are recognized by Windows Command Prompt but not by WebStorm's terminal. I get error:
'node' is not recognized as an internal or external command
Does anyone know what is problem? I could use WebStorm's terminal normally till today, but something went wrong and now I'm stuck.
EDIT:
echo %PATH% screenshot:
Settings/Tools/Terminal
screenshot:
I had similar issue. I had to select File -> Exit from the IntelliJ application as opposed to using IntelliJ's internal restart option: File -> Invalidate Caches / Restart -> Just Restart.
I had the similar problem like this.I was working with my project with another computer. and after that i decide to work with this project with my computer.so after moving the Files of project. i installed the Node.js and i had all of the paths about Node but when i was want to Run the project. i had this problem. i search a lot but nothing works. so i guess maybe when i restart my computer the new paths of variable system will work well. i restarted and my problem solved!
For those who are encountering this problem when you try to edit the run configuration, you have to put "start" as the command instead of "npm". By default Webstorm knows it's a npm project so it already starts with npm, you simply have to put the start command after that.

command not found: nodemon --- need helping adding directory to PATH

I have looked at a lot of answers on here and I'm still not able to figure this one out. I am attempting to learn node again, but after my global install of nodemon (which completed successfully) I am getting command not found: nodemon when running nodemon app.js. A while back I moved my npm path to /Users/mlefkowi/npm-global for some reason. When I echo $PATH, that directory does not exists. This is what I get:
/Library/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I have been able to add it to the $PATH, but when I close out Terminal it doesn't save. Should I move npm back to it's default directory? How can I get a path to save to $PATH?
Edit your ~/.bashrc and add the following line:
export PATH="/Users/mlefkowi/npm-global:$PATH"
This assumes that /Users/mlefkowi/npm-global/nodemon is the path to the nodemon executable. If it's in a bin sub-directory off of npm-global, then change /Users/mlefkowi/npm-global to /Users/mlefkowi/npm-global/bin.
.bashrc is sourced every time your shell starts up, so this modification will stick for all new terminal sessions. After editing it for the first time, run source ~/.bashrc to update your PATH for that session.

EPERM error when running Gulp as non-root

I'm using Gulp to compile some assets on a simple standalone WordPress theme project.
When I run my gulp task that compiles some LESS files I get the following error:
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: EPERM, chmod '/Users/harryg/Sites/sites/wordpress/wp-content/themes/samarkand-2/assets/css/main.min.css'
When I run it as sudo the task executes without problem.
Thinking it was a permissions error I chmod'd the entire theme folder and its contents to 777 but this doesn't solve the problem. I have gulp installed globally which may be the issue but I'm not sure how to solve.
EDIT
Even if I run the local gulp I get the same error. I.e. running node_modules/.bin/gulp from my project folder produces the same EPERM error.
`sudo chown -R `whoami` sites/wordpress/wp-content/themes/samarkand-2`
should fix it. Or whatever directory you want to start from. This is no dangerous and might come because of you node-installation. It's pretty common and should be fixed in general after running installs with npm#3.X.X
While #cwelske gives a "hacky" workaround in his answer, the link he provides to the bug report provides a nicer fix for Gulp 3: use vinyl-fs.
npm install vinyl-fs --save-dev
Modify your gulpfile to use VFS:
Add to your requires block: var vfs = require('vinyl-fs');
Modify your output pipe: .pipe(vfs.dest('./output'));
The reason I was facing this error is because the files were owned by a more generic group and user so that multiple team members could work on the project together. Obviously Ubuntu doesn't allow you to chmod someone else's files. I suspect the OP is in a similar situation (the web-server might own the files).
If you do not require this kind of setup, your other option is to change the files to be owned by your user, something like:
sudo chown -R me:me .
Setting file permissions to 777 are not going to help you, because unless you own the file, you cannot chmod it without sudo. (Hence it works when you run gulp as sudo.)
gulp 3.x has a bug that makes it want to chown all files regardless if it already has all required permissions or not. It is supposedly fixed in gulp 4.
The linked bug contains a nice workaround if you know that the files have the correct permissions: Simply replace fs.chmod with an empty function in your gulpfile.js:
var fs = require('fs');
if (1) fs.chmod = function (a, b, cb) {
cb(0);
}

Sails.js - PATH variable - sails command not recognized

After I npm installed Sails.js on Windows Server 2008, "sails" command is not recognized.
Can someone give me a hint on what values to use in the PATH variable? As I understand it is Node.exe that runs the sails.js file. But if I try tunning "node sails.js" command in cmd, it recognizes it, but can't find some of the dependencies.
On my Windows 7 machine everything installed and is running like a charm.
I ended up writing a batch file and putting it into system32 folder "c:\Windows\System32\sails.bat" with this one line:
node C:\Users\XXXXXXXX\AppData\Roaming\npm\node_modules\sails\bin\sails.js
Now, sails lift works well.
Did you try using the -g (for global) option?
If I use:
npm install -g sails
On either Windows 8.1 or Server 2012R2, I find it is accessible from the path just fine.
Install sails globally
npm install -g sails
If you have right to add Environment variables (Start => Computer=> Properties=>Advance system setting => Advance(Tab)=>Environment Variable(button at the bottom) => User variable for ...(the top one) => find "PATH" => edit ) and add the location of your npm folder (C:\Users\XXXXXX\AppData\Roaming\npm) (this folder can be hidden so enable show hidden folder to locate your path)
open new cmd window and enjoy sails :)
it looks line a sails.cmd file is create in the global npm folder, so if you add this folder C:\Users\XXXXXX\AppData\Roaming\npm to your PATH sails will be a recognized command, and will accept all valid parameters (tried new and lift and both look OK).
In this way all other node packaged that are command line based should work, if they follow this convention to install on Windows (I think this is the case).
I don't have a Windows Server 2008 to test on but it seems it fails to do this process automatically, as it does on Windows 7
If you add a .bat file in system32, remember to add parameters to the bat script. Found that out after trying some of the solutions previously posted here. My example:
C:\Users\Anton\AppData\Roaming\npm\sails %1 %2
Sails is not a directory there, it's a file. Hope this helps someone.
For ubuntu 16.+
Get prefix of node
npm get prefix
look loke this : '/home/ubuntu/node'
now open bash_profile
sudo vim ~/.profile
Add this line if you already have path in this file
export PATH="$PATH:/home/ubuntu/node/bin"
if you node prefix is diff replace with your prefix
export PATH="$PATH:{ your-node-prefix }/bin"
you can add new 'node' commands like 'npm', using batch scripting.
Create a sails.cmd file in your desktop. (new text file -> rename to
'sails.cmd').
Right-click on sails.cmd and select Edit.
Copy and paste this code into your sails.cmd:
#ECHO OFF
SET arguments=%1 %2 %3 %4 %5 %6 %7 %8 %9
node %APPDATA%\npm\node_modules\sails\bin\sails.js %arguments%
Save and copy your sails.cmd into your Node.js installation
directory, for example: C:\Program Files\nodejs
And now everything with sails.js on windows are very simple. Just run this command in your terminal:
sails or sails -v or whatever you want with sails! ;)
Enjoy!
UPDATE!
At this time I highly recommend using the WizofOz solution. It is the most correct way to proceed in a node.js environment.
To help clarify for Windows 8 users. After installing sails globally if receiving errors do this:
Create a sails.txt file
Add this
node C:\Users\XXXXXXX\npm\node_modules\sails\bin\sails.js new
node C:\Users\XXXXXXX\npm\node_modules\sails\bin\sails.js lift
Now re-save it as sails.bat
Add this new file to your C:\Windows\System32
Navigate to the folder you want to hold your new sails app. Scaffold the app with the following command:
sails new
If you don't want a front-end, instead type:
sails new --no-frontend
We can also launch the app using the lift command:
sails lift
This will launch our new app on port 1337
For MAC Users, try this (run the following on your terminal)
export PATH="/Users/username/.npm-packages/bin:$PATH"

Grunt on Windows 8: 'grunt' is not recognized

I'm having a problem running Grunt from the command line on my Windows 8 machine.
My research indicates the most common solution is to install grunt-cli, since Grunt is no longer global. I also need to make sure I actually install the Grunt task runner, since that's not installed with grunt-cli.
Other solutions point to the PATH system environment variable, but that appears to be pointed as I'd expect to:
C:\Users[username]\AppData\Roaming\npm
Having done all that, I'm still getting a "'grunt' is not recognized as an internal or external command, operable program or batch file" error message in the CLI. I've tried the following things, uninstalling everything after every attempt:
Installed grunt-cli globally (npm install -g grunt-cli), then grunt at the directory level I want to use it (npm install grunt)
The same as above, but with the order of installation reversed
The same as both of the above, but using the Admin Command Prompt
Am I missing something obvious?
I've not had any issues with grunt on several different windows 8 machines.
If you open the folder: C:\Users\[username]\AppData\Roaming\npm
Do you have a file named grunt.cmd in this folder?
If not I'd maybe try npm install -g grunt-cli again, maybe from an elevated command prompt.
If this exists and you have C:\Users\[username]\AppData\Roaming\npm in your PATH environment variable then typing grunt from a command prompt should work.
Silly question, have you tried closing the command prompt and opening a new one?
Confirm your PATH is correct (and not messed up). Just type PATH from the command prompt. There's really no other explanation that makes sense given the error you're describing and the steps you've taken.
Normally, using the where grunt command would have found grunt.cmd in your path if npm is installed correctly and it has been properly added to the system path.
Close all Command Prompt instances.
Start a new Command Prompt instance.
Type PATH Enter and verify if C:\Users\Username\AppData\Roaming\npm is part of the path.
If not, you need to log off and on again,or close the Command Prompt and restart the explorer process.
In the Command Prompt, type where grunt Enter.
You're good if it reports:
C:\Users\Username\AppData\Roaming\npm\grunt
C:\Users\Username\AppData\Roaming\npm\grunt.cmd
Otherwise, you have to reinstall the grunt-cli package if it reports:
INFO: Could not find files for the given pattern(s).
Apparently, programs that change the PATH environment variable must broadcast a WM_SETTINGCHANGE message. The Windows' System settings window does it correctly when you change the PATH variable, but the NPM installer doesn't. That's why you have to restart explorer (or log off or restart, which has the same effect).
I know this has been answered but I thought I'd offer my step by step solution for windows 8.
First thing I checked was the PATH in my laptops Environment Variables (Right click my computer > properties > advanced system settings > Environment Variables)
It wasn't listed in there so I added a new variable in User variables (so it was specific only to my user account)
In the new user variable prompt I entered the following;
Variable Name: PATH
Variable Value: %USERPROFILE%\AppData\Roaming\npm
Quit command prompt, repoened, navigated to my projects directory and tried running grunt again and... SUCCESS!
I had the same issue.
I tried different things:
Restart computer
Deleted the grunt folder and ran
npm install -g grunt -cli
Didn't work.
Finally tried:
npm install -g grunt-cli
Worked perfectly.
Tried
where grunt
and I saw 2 locations where it was found.
I was facing the same problem on windows 8
I have added ' %APPDATA%\npm ' to the path variable . It has been working fine.
some times NPM install corrupts the basic windows path. i usually have a copy of my own version of PATH mainted separately. every week or on some installs i manually configure and update the %PATH% variable.
Basically Grunt.cmd is not availbe through %PATH% variable.
I have stucked with problem on Windows 8, that after install grunt-cli I've always got "command not found" while I'm tried to check grunt -v or where grunt. So I've added to enviroment PATH this path C:\Program Files (x86)\Git\local and run grunt.cmd from that folder (you need to look in node_modules folder here). And after reloading my terminal everything started to work.
Same happened to me and here was the solution:
Have you got 2 different versions of Node.JS installed?
Maybe Nodist?
This means you likely got NPM installed twice which will install the commands into 2 different folders:
Once into C:\Users\<user>\AppData\Roaming\npm and once into C:\dev\nodist\bin\bin.
C:\dev\nodist\bin\bin wasn't on my path variable so I added it, and I removed the Node.JS version I didn't want to use.
If you have no grunt.cmd file created by npm, make sure that you do not have a .npmrc in your home directory with: bin-links=false in it.
After getting a tonne of "'grunt' is not recognized as an internal or external command," errors, I solved this on Windows 10 by going to Path and adding C:\Users\Username\AppData\Roaming\npm

Resources