where should a module write files? - node.js

I wrote a CLI module, wikidata-cli, that for a certain command (wd props) caches results as files in the module directory: node_modules/wikidata-cli/props/some-file. It works in my local setup where node and npm were installed in my home folder (using nvm), but other people having installed node/npm from their package manager, probably with sudo rights, encounter issues: once installed, the module loses the permission to modify the module directory and they get errors of the kind EACCES: permission denied, open '/usr/lib/node_modules/wikidata-cli/props/de.json'
I tried changing the access permission during the postinstall script - "postinstall": "mkdir -p props && chown -R 666 props" - but had to revert it was blocking any installation with a operation forbidden error.
Any clue were this kind of file is expected to be written in a cross-platform compatible way?

If you're looking for the code to be able to write files no matter which user runs it, then there's really only one answer: The only folder that the current user a) is guaranteed to be able to write to, and b) won't encounter conflicts between different users accessing the same paths, is the user's own home directory.
You could use something like this to derive a path that the current user can definitely access:
var homePath = process.env.HOME || process.env.USERPROFILE; // POSIX || Win32
var moduleDataPath = path.join(homePath, ".wikidata-cli");
if (!fs.existsSync(moduleDataPath))
fs.mkdirSync(moduleDataPath);

Related

Lack of permissions in Windows

I created C:/Users/homedir/Documents/Python. Windows automatically
puts that in OneDrive's path. Is this causing the permission errors?
d#LAPTOP-M0R87BQ6 MINGW64 ~/OneDrive/Documents/Python/twilio-python-main (main)
$ python3 setup.py install
running install
error: can't create or remove files in install directory
# The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\\Lib\\site-packages\\test-easy-install-9920.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\Lib\site-packages\
Is the only problem I need to create the directory (or modify setup.py to install in Python's path elsewhere)?
Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.
I am the Admin for this machine, I'm in the Group Administrators. There is no higher root on my machine. I opened a PowerShell as an Admin and ran the python interpreter. Still insufficient permissions. Looking at Windows Explorer I am Owner of all the files.
The error says: "If you do not have administrative access to this machine, you may wish to choose a different installation directory, preferably one that is listed in your PYTHON PATH environment variable."
For information on other options, you may wish to consult the
documentation at:
https://setuptools.readthedocs.io/en/latest/easy_install.html
Please make the appropriate changes for your system and try again.
in cmd windows try taking ownership of WindowsApps folder and ensure the properties are inherited by its subfolders
takeown /f \"C:\Program Files\WindowsApps" && icacls \"C:\Program Files\WindowsApps" /grant administrators:F

Installing software on server machine - the install process wants to modify root folders which I cannot access

I'm configuring software on my first web server, so I am not totally familiar with how everything works, but here is the basic problem:
I have purchased hosting on a web server that runs on CentOS. I have been able to install postgreSQL via an install process that the hoster provides, so that my database will be local only to my home folder. That is working fine.
However, I am trying to install a postgreSQL extension called PostGIS. I have tried to compile it from source on my web server, but it now requires an additional library called GEOS. I downloaded the library from http://download/osgeo.org/geos/geos-3.6.2.tar.bz2, extracted it, and used make install to run it.
Now the problem is that it fails due to this error:
/bin/mkdir: cannot create directory /usr/local/include/geos: Permission Denied
It's not really a surprise, because it is trying to make a new directory in the system root folders, rather than within my personal home folder, which is the only one I have access to. I can't think of any other way around this. Am I just unable to install this library? Or can I "trick" it somehow into installing in in my home directory, where I have full admin rights?
I think You need to execute a command with root user privileges.
Because, make install command need root user privileges.
Like,
sudo make install
or with root user. Like,
sudo su
make install

Workspace Settings permission denied

I'm running Ubuntu 12.04LTS.
Have unpacked Visual Studio Code in a folder owned by my user id. All vscode files are owned by my user id (user and group).
Have Node.js, npm, typescript installed via apt-get (and npm).
Visual Studio code runs fine, however File->Preferences->Workspace Settings gives this error:
Unable to create 'vscode/settings.json' (Error: EACCES: permission denied, mkdir '/.vscode').
Any ideas on how to resolve this? Where is it trying to do the mkdir?
Thanks,
Bob Wirka
UPDATE: Sudo'd mkdir "/.vscode" (literally at the root level), and chown'd it recursively to my user and group. Voila! Now I can edit the settings.
So, is there a way to tell Visual Studio Code that it shouldn't be trying to use the root folder?
Mentioned in the update by the OP but thought I'll mention it explicitly. You need to change the permissions for the folder. The following command will change the owner of the directory so that you can open it without needing root privileges.
$ sudo chown <user-name> -R <directory-name>
I had same issue on my osx. I was able to solve this issue by change the permission to read and write in project folder.
Simply type
sudo chmod 777 -R <your_app_name_directory>.
This will give all permissions to all users, groups and others for read, write, execute.
-R gives recursively permissions to all nested files folders inside your directory.
If -R is not given then it gives permissions to current directory only, not to other directories inside.
Change the permissions to your folder
sudo chmod ugo+rwx your_folder

Linux directory permissions issue (NPM's node_modules directory)

I'm trying to figure out how to get the permissions to work for me with my node_modules directory. Here's the problem...
user1 goes to a folder with a package.json and runs npm install resulting in a node_modules directory with the owner and group user1.
user2 then goes to that folder (it's a shared folder) and tries running something like npm install --save some_dep, but they don't have permissions on this directory so it fails.
Okay, that's fine.. so I chown -R the node_modules to be a shared group which both users are in.
Now user2 can run his command successfully resulting in a folder node_modules/some_dep. But now user1 doesn't have access to this folder.
So I try using a setgid permission on the node_modules directory. Something like chmod -R g+s node_modules. The result is exactly the same as before - when a user runs a NPM command the resulting directory gets the wrong group and permissions.
So I tried using ACLs, but the result is again the same.. the directories resulting from a user running an NPM command will not respect the permissions of the host folder.
Maybe this has something to do with how NPM dependencies are built. Maybe they are built elsewhere and then moved?
Is there any way to set permissions that are effective for that directory and all future subdirectories in such a way that I can make this work?
I could just run everything as root all the time... but this is clearly not ideal.
This appears to be due to a design flaw in npm. When creating the node_modules folder, npm (for reasons no one can explain) executes chown on the folder explicitly. Rather than relying on the OS to properly set permissions, npm mucks with problems, messes up your permissions, and makes it pretty much impossible to use it in a shared environment without periodic manual runs of chmod/chown.
This is likely the source of the endless trail of permissions-related issues people have trying to use npm.
Developers promise (for the nth time) this problem is fixed in later versions: https://github.com/npm/npm/blob/db9cde008ce855bdac801bb6649cbfb5bb7911ac/changelogs/CHANGELOG-2.md#v2120-2015-06-18

npm install failed because of virtual directory

I'm trying to setup a new project which is a git repo stored on a virtual drive (wuala).
Is it possible that this is not possible? Because when I try npm install I get the following error:
node.js:815
var cwd = process.cwd();
^
Error: ENOENT, no such file or directory
at Function.startup.resolveArgv0 (node.js:815:23)
at startup (node.js:58:13)
at node.js:906:3
Ok, so the issue was node not finding the current working directory. I didn't know it was only a virtual directory, after downloading the folder locally it works.
Now if only there is way to download the folder locally every time wuala is running & to be deleted afterwards...
Could the problem be that Wuala doesn't accept empty folders? This topic seems to suggest non-standard behavior when it comes to empty folders:
https://support.wuala.com/forums/topic/wuala-does-not-accept-an-empty-folder-on-my-lacie-2big-as-sync-partner/
I came across this issue today and I don't think its related to the file system being virtual or not. The answer is really simple and weird.
I followed these steps to reproduce the issue:
Open a Terminal and cd to a directory (let's say /home/user/dir)
Delete the directory from your file manager (nautilus in my case)
The console still shows :
user#user-PC:~/dir$
although the directory doesn't exist.
Run a node command. I ran sudo npm update -g cordova
You get the above error. So, the solution is as simple as being in a directory which exists and then executing the node command.

Resources