node.js - How to write to a file in a globally-installed Node module? -


reference

I am developing to be installed as a CLI executable. Therefore, I am packing it for use with NPM, and to install it.

Problem

However, as a CLI program, I want to write in a log file. This works fine on local installs, but certainly less accurate in a global installation: the program can be used as the / usr / local / lib / รข ?? | is placed in NPM, which has to be run sudo to write there.

Then, when a user tries to run the device, it fails due to EACCES : The log folder and the files can not be created.

Steps taken

I still ( mkdir log and end log / exec .log ), but those files are then any is not owned by, and can not be opened by the tool. The problem varies slightly but ends very soon.

Question

Is there a way to authorize the NPM module to write in its own directory?

more specific questions

What advice would you give me? I have thought about the following solutions:

  1. Write the log file in the execution CWD It is ugly, and not so secure because CWD may not be too writable.
  2. Write a file in a preset writeable log file, such as ~ / .mytool / log , but I do not like all the user's files on all the spreading files on the machine .
  3. Ask the user for a log file destination. Extremely annoying
  4. chmod 666 global log file. Less ugly but unsafe

    Is there any common thing to handle this kind of issue?

    No, allowing anyone to write in a directory globally installed node in their own directory There is no way.

    In fact, to allow a global setting, i.e. write to them / usr / local / lib , root via NPM via sudo Can be run as. However, while doing so, the NPM will be:

    According to Version 0.3, it is recommended to run NPM in the form of root. This NMM allows to change the user identifier in any any user before running any package build or test order.

    Everyone nobody , and therefore can not override this setting.

    This is a security feature that prevents any unauthorized post-install modifications in an installed module, making it trustworthy on the installed module.


    You should try to find a default location for your log, which is understandable in your business logic. User-overdrydable (through CII switches) would be good for global writing logs like ~ / .yourTool / log , but does not extend node ~ , so cross-search - The platform seems to be a bit tricky to the global logging destination.

Comments