EPERM error when running Gulp as non-root - node.js

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);
}

Related

/usr/bin/env: node: Permission denied

As the title suggests I am receiving the error
/usr/bin/env: node: Permission denied
when trying to run
npm run build
for my react app. The following link is the leadup to this point with all that I've done and tried, not to be included here for the sake of not duplicating questions Error with react-scripts in npm run build.
Additionally, I have viewed this posting, Getting Error /usr/bin/env: node : Permission Denied, and it was unhelpful to me.
Any help is much appreciated.
I was working in a dockerized node environment and noticed that the mounted source code directory had different ownership (running the container as root) from host environment. Changing the ownership using:
chown -R root:root .
made the error go away for me!
The issue ended up being a matter of file ownership. Root was the owner of many things for the project rather than my profile. Simply changed the ownership from root to me.
For me, re-installing node with NVM solved the issue:
Install nvm
nvm install 14
nvm use 14
you should running command as super user sudo npm run build
For anyone else hitting this, there is another avenue of investigation. There might be another factor affecting execute permissions. There's an exec flag on drive mounts to enable script execution (neat safety feature for automounted usb sticks!). So if your code is on a drive that you've mounted, you need that option set to run scripts on it. Tell-tale signs are the permissions on the script being correct (appropriate user, group and execute bits set), but it still complaining about the shebang line (1st line) being the permission denied. Also, if you type mount, you might see noexec set on the mount point where your source code is (it may not show here if exec is set). How your mount is set up varies greatly, but a place to start is your /etc/fstab.

strange npm problem, command never finishes, hangs forever

This is windows 10, node-v10.15.3-x64.msi install.
using either command prompt or power shell I type npm and nothing happens and it doesn't bomb or returns to the prompt. Just the little blinking dot that lies to me saying it is doing something. When I stop being fascinated by this blinking dot and hit control c it says Terminate batch job (y/n) ? y gives me back my prompt. Obviously there was some process running but I am not getting a functioning program. Any ideas? I know this is the head scratch kind of problem that does not usually get answered but I can't be so special nobody else has this happen.
What underlying tech does npm rely on? python?
Ok with help from the other thread I got it figured out.
Yes I screwed with prefix to my detriment. The reason why I kept going round and round deleting and reinstalling is I missed .npmrc in my user directory. The prefix in it was set to a path that no longer existed. I deleted my profile .npmrc and it started working again. I have now learned that is the file to edit to set prefix and cache.
Now the structure of the nodejs stock install for npm is bizarre imho. The npm executables are in the nodejs directory. Node itself is installed nodejs/node_modules/npm. The npm modules are path nodejs/node_modules/npm/node_modules.
Initially I changed my personal .npmrc to this contents
prefix="C:\Program Files\nodejs\node_modules\npm"
cache="C:\Program Files\nodejs\node_modules\npm"
This stopped npm from installing into my appdata/roaming directory but it kept bombing because it could not create the cache directories because it was in Program Files
But I was happy because it was trying to install global into one directory instead of my profile roaming. The main reason I started this odyssey is I wanted global modules in ONE location.
So what I did was moving nodejs from program files to another directory.
change my profile .npmrc to this
prefix="C:\nodejs\node_modules\npm"
cache="C:\nodejs\node_modules\npm"
changed the path entry for nodejs in paths in system properties / advanced / environmental variables / system variables / path to C:\nodejs.
I also keep deleting path in the top path in user variables but it seems to keep coming back like Freddy Krueger. Doesn't seem to do any harm now though.
A lot of examples try to set prefix to nodejs\npm. Maybe that was kosher at one time but now the npm executable called npm is in the nodejs root directory.
Okay I am not a tech writer but I hope this will provide clues to other clueless like me. Cheers!

How to tersely run a globally installed Node.js app

I was recently using mongo-express which is written in Node.js. I've installed it globally following instructions on their GitHub page. It says in order to run it:
cd YOUR_PATH/node_modules/mongo-express/ && node app.js
I was wondering if there's any workarounds to avoid changing directory to the app's folder first (using cd).
By the way, in npm's doc, it says
When in global mode, executables are linked into {prefix}/bin on Unix.
In my case (using mac), executable app.js is linked into /usr/local/bin. But if I just type app.js in the command line trying to run it, it'd say -bash: /usr/local/bin/app.js: Permission denied. Even if this works, wouldn't app.js be a too generic name and become conflicted with other apps later?
Thanks for helping.

Permission denied when running NPM postinstall script?

I have a postinstall script in my package.json file for a node module, that locates a file within the node module and copies it to another directory. if I run the post install script by itself, it executes without issue. However, when I make a tarball of the module and try to install it in another repo, I get a permission denied and exit status 126 message. I tried simplifying the file to a single console.log("foo") line and it still fails to execute. I have manually checked all permissions the target directory and all its parent directories; they are read-write for everyone (Mac). Has anyone encountered this?
I hadn't found this thread until it showed up as related to my posted question, but this solved my problem.
npm install - how to run build scripts with sufficient permissions?
Thanks to Pascal Belloncle

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

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

Resources