Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/username
Get the username of the current user
https://github.com/sindresorhus/username
Last synced: 3 days ago
JSON representation
Get the username of the current user
- Host: GitHub
- URL: https://github.com/sindresorhus/username
- Owner: sindresorhus
- License: mit
- Created: 2014-03-30T18:12:49.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T16:18:47.000Z (about 1 year ago)
- Last Synced: 2025-02-01T06:08:08.459Z (10 days ago)
- Language: JavaScript
- Size: 36.1 KB
- Stars: 142
- Watchers: 6
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-cross-platform-nodejs - username - Get the current username. (Libraries / Environment)
- awesome-micro-npm-packages - username - Get the username of the current user. (Modules / CLI)
- awesome - sindresorhus/username - Get the username of the current user (JavaScript)
- awesome-nodejs - username - Get the current username. ![](https://img.shields.io/github/stars/sindresorhus/username.svg?style=social&label=Star) (Repository / Environment)
- awesome-micro-npm-packages - username - Get the username of the current user. (Modules / CLI)
- awesome-micro-npm-packages-zh - username - 获取当前用户的用户名. (模块 / CLI)
README
# username
> Get the username of the current user
This module is meant for informational purposes and not for secure identification.
## Install
```sh
npm install username
```*This package only works in Node.js, not in browsers.*
## Usage
```js
import {username} from 'username';console.log(await username());
//=> 'sindresorhus'
```## API
It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached.
### `username(): Promise`
Returns the username.
### `usernameSync(): string | undefined`
Returns the username.
## Related
- [username-cli](https://github.com/sindresorhus/username-cli) - CLI for this module
- [fullname](https://github.com/sindresorhus/fullname) - Get the fullname of the current user