4

I feel like this is a silly question but I can't get node to run a .noderc file, not even to just log to the console and not even on linux where I would expect everything to work.

My use case is that for work I have to use Windows and npm has installed modules to a particular location (%HOME%\AppData\Roaming\npm\node_modules\), whereas the default module.paths in node does not include that directory. I am fine with the location, so I don't want to fix this on the npm end of things. I have easily fixed the issue by appending this path to modules.paths, so the real solution should be for me to add that to an rc file.

I tried making a .noderc in my Windows home directory, and to my surprise it seems to not be running. I did the same on my personal laptop running a linux distro (~/.noderc) and the same thing happens. A log to console or definition of a test var does not show up in the REPL.

Is there something obvious I am missing? Usually programs have a hierarchy they run through, with default configs, a system level config file (if it exists), and a user level config file (if it exists). In the case of a program like X, they are executed in order and overwrite options, where as in something like bash, they are checked in reverse order and the first one found is executed (it is common for the first line of a user level bash config to source the system level one). How does node function?

EDIT: In the comments below where I link to an old SO thread I noticed that there is a bit of a hack involving an alias to get the .noderc to work. So I guess a better question is, how are things like module.paths configured in node? There must be a way not involving a full rebuild.

  • It's the first time I've heard `.noderc` exists. I'm interested to see where did you find info about it? – Tsvetan Ganev Nov 18 '21 at 20:20
  • Well, its an old thread but you can reference here: https://stackoverflow.com/questions/23036753/how-do-i-set-up-a-preload-file-for-node. Maybe it has changed? Maybe it no longer exists? But how would you handle config options like the module search path? Certainly that must be configurable. EDIT - I see in one of the comments how they hacked that one together. So is there no config for node? – Poisson Aerohead Nov 18 '21 at 20:40
  • So far the only thing I can find is an environment variable that at least solves my issue. I am surprised but it looks like node might have no config file. I'll leave this for the two days to see if anyone else responds before I answer my own question. https://nodejs.org/docs/latest-v14.x/api/cli.html#cli_node_path_path – Poisson Aerohead Nov 19 '21 at 03:51

1 Answers1

1

As there has been no answer for over 10 days, I am going to just post my workaround form the comment above. It looks like there is no node config file. Any further info on that welcome. In order to solve my particular problem, I used the NODE_PATH environment variable.

I personally prefer to use use config files and not environment variables for scripting issues that need to be addressed every time. Config files are always read automatically, while using an environment variable requires you to always remember to add the variable or to permanently add the variable to your environment (which clutters the environment). I prefer to restrict environment variables to specific variations from the default. However, as I said, I can't find a config file for node.

  • A bit late to the party, but you can also use [direnv](https://direnv.net/) to unclutter you global environment while still having locally available environment variables. – João Carmo Dec 29 '22 at 09:54
  • Interesting to know, thanks. For my use case I just workaround by SETting the environment variable when I need to run something locally. I am not a full time dev anyway so it does not really matter. – Poisson Aerohead Jan 12 '23 at 06:01