Giter Club home page Giter Club logo

env-paths's Introduction

env-paths

Get paths for storing things like data, config, cache, etc

Uses the correct OS-specific paths. Most developers get this wrong.

Install

$ npm install env-paths

Usage

import envPaths from 'env-paths';

const paths = envPaths('MyApp');

paths.data;
//=> '/home/sindresorhus/.local/share/MyApp-nodejs'

paths.config
//=> '/home/sindresorhus/.config/MyApp-nodejs'

API

paths = envPaths(name, options?)

Note: It only generates the path strings. It doesn't create the directories for you. You could use make-dir to create the directories.

name

Type: string

The name of your project. Used to generate the paths.

options

Type: object

suffix

Type: string
Default: 'nodejs'

Don't use this option unless you really have to!

Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it.

paths.data

Directory for data files.

Example locations (with the default nodejs suffix):

  • macOS: ~/Library/Application Support/MyApp-nodejs
  • Windows: %LOCALAPPDATA%\MyApp-nodejs\Data (for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Data)
  • Linux: ~/.local/share/MyApp-nodejs (or $XDG_DATA_HOME/MyApp-nodejs)

paths.config

Directory for config files.

Example locations (with the default nodejs suffix):

  • macOS: ~/Library/Preferences/MyApp-nodejs
  • Windows: %APPDATA%\MyApp-nodejs\Config (for example, C:\Users\USERNAME\AppData\Roaming\MyApp-nodejs\Config)
  • Linux: ~/.config/MyApp-nodejs (or $XDG_CONFIG_HOME/MyApp-nodejs)

paths.cache

Directory for non-essential data files.

Example locations (with the default nodejs suffix):

  • macOS: ~/Library/Caches/MyApp-nodejs
  • Windows: %LOCALAPPDATA%\MyApp-nodejs\Cache (for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Cache)
  • Linux: ~/.cache/MyApp-nodejs (or $XDG_CACHE_HOME/MyApp-nodejs)

paths.log

Directory for log files.

Example locations (with the default nodejs suffix):

  • macOS: ~/Library/Logs/MyApp-nodejs
  • Windows: %LOCALAPPDATA%\MyApp-nodejs\Log (for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Log)
  • Linux: ~/.local/state/MyApp-nodejs (or $XDG_STATE_HOME/MyApp-nodejs)

paths.temp

Directory for temporary files.

Example locations (with the default nodejs suffix):

  • macOS: /var/folders/jf/f2twvvvs5jl_m49tf034ffpw0000gn/T/MyApp-nodejs
  • Windows: %LOCALAPPDATA%\Temp\MyApp-nodejs (for example, C:\Users\USERNAME\AppData\Local\Temp\MyApp-nodejs)
  • Linux: /tmp/USERNAME/MyApp-nodejs

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

env-paths's People

Contributors

bendingbender avatar borekb avatar richienb avatar siilwyn avatar simonkberg avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

env-paths's Issues

Incorrect paths on Linux

If e.g. $XDG_CACHE_HOME is set, it will return XDG_CACHE_HOME instead of XDG_CACHE_HOME/{name}as seen here.

Is this intended, and if not would you accept a PR to fix this?

make the -nodejs suffix configureable

I need a tool just like this for use on an electron project, but the -nodejs suffix will get in the way.

I could just replace() the output, but an option to specify the suffix in this lib would be nice.

On Windows, config should go into `env.AppData`

%AppData% on windows rooms across the network/domain and is more likely to be backed up. For configuration (small user specific files managed by the application), this should go into AppData.

%LocalAppData% is per machine (not synced across network on login/logout), and so cache, program data, and logs are correct to go in there.

Version 3.0.0 breaks packaging with vercel pkg

Hi! This is a great package, thanks for creating it.

Version 3.0.0 (specifically, commit 995e663) breaks packaging with vercel pkg (https://github.com/vercel/pkg).

It looks like vercel pkg is choking on the node:path import syntax introduced in that commit.

Relevant snippets of pkg debug output:

> [debug] Bytecode of node:path is added to queue. It was required from <snip>/node_modules/env-paths/index.js
> [debug] Bytecode of node:os is added to queue. It was required from <snip>/node_modules/env-paths/index.js
> [debug] Bytecode of node:process is added to queue. It was required from <snip>/node_modules/env-paths/index.js
> [debug] Stat info of <snip>/node_modules/env-paths/package.json is added to queue.
> [debug] Content of <snip>/node_modules/env-paths/index.js is added to queue. It was required from <snip>/node_modules/env-paths/package.json
> [debug] Content of <snip>/node_modules/env-paths/index.d.ts is added to queue. It was required from <snip>/env-paths/package.json
> [debug] Content of <snip>/node_modules/env-paths/package.json is added to queue.
> [debug] Stat info of <snip>/node_modules/express/package.json is added to queue.

And then later:

> [debug] Directory <snip>/node_modules/env-paths is added to queue.
> [debug] Stat info of node:path is added to queue.
> [debug] Content of node:path is added to queue.
> Error! Cannot read file, ENOENT
  node:path
error Command failed with exit code 2.

Namespaced directory names?

For example envPaths('unicorn').config outputs /Users/sindresorhus/Library/Application Support/unicorn on macOS. Imagine if a desktop app is named unicorn too. There would be conflict as desktop apps have config there too. Not very likely, but not sure I'm willing to take the risk either.

Do you think they should be namespaced? For example: /Users/sindresorhus/Library/Application Support/nodejs-unicorn? That would remove all possible conflict, although more ugly.

tl;dr unicornnodejs-unicorn

Is linux userspace log path really ~/.local/state

Hmmm...

At least on Ubuntu there is no ~/.local/state directory by default so if I try to use envPaths(opts.appName || opts.name || 'default').log)

I get an error cause it can't write to a non-existent directory.

Not that I or the end user can't make that directory or that I won't now check to see if it exists first and make it before the error is thrown (make-dir to rescue!)

but I have two comments.

  1. I can't find anywhere that states that ~/.local/state is an agreed upon location for userspace logs in Linux. I'm not opposed to this just can't find any justification for it. And why the cryptic state instead of log??
  2. More typically they should probably go in shared var/log/<appname> space with appropriate permissions. Obviously there are permission issues here but at least one would get the typical applications log path from which to proceed (at least for Linux)

still wondering if making missing directories is within the purview of this package??? Maybe with extra option mkdir:true which is false by default so it won't be done unless the dev really wanted it so.

Prefer XDG_* on all systems

If a user explicitly defines an XDG_* environment variable on macOS or Windows, we should use those XDG_* dirs. It could also be an option, disabled by default?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.