GithubHelp home page GithubHelp logo

username's Introduction

username

Get the username of the current user

This module is meant for informational purposes and not for secure identification.

Install

npm install username

This package only works in Node.js, not in browsers.

Usage

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<string | undefined>

Returns the username.

usernameSync(): string | undefined

Returns the username.

Related

username's People

Contributors

arthurvr avatar bendingbender avatar elvinn avatar kevva avatar raido avatar richienb avatar sindresorhus avatar sonicdoe avatar sonnyp 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

username's Issues

Clarification about working in the browser

Hello!

After playing around a bit with your package (in the browser), I realized that it may or may not have been designed to get the username in the context of a browser.

My question is, "Is this package able to get usernames in the browser at all?" I can't seem to get it to work in Windows 10, and I have a suspicion that it can't on any OS, but I'm not familiar with Linux or Mac browsers, so I thought I'd ask before assuming. The more I write, the more I feel rather silly for trying so long at it... ಠ_ಠ

Maybe I could add a line to the README to clarify to my fellow knaves that this package was designed to only get a username from non-browser apps.

Give me a hand, what would such node applications be called that don't run in the browser? Desktop applications? Command line apps? Are there non-browser, non-command-line apps in Node?

I appreciate you humoring me.

os.UserInfo does not honor SUDO_USER

if you are using this in a script called with sudo, it'll always return root on linux because os.UserInfo doesn't honor SUDO_USER. It would be nice to return SUDO_USER first if available instead of UserInfo

npm shrinkwrap error

Hi all,

During the getting shrinkwrap file by 'npm shrinkwrap', it corrupts and logs the following:

npm ERR! node v5.5.0 npm ERR! npm v3.3.12 npm ERR! Problems were encountered npm ERR! Please correct and try again. npm ERR! extraneous: [email protected] <PATH>

That's why, I cannot reproduce the shrinkwrap.json file to create a dependency tree file for the application.

Thanks.

User x Root

I use this lib in a project, and until previous version, it works. But now, the behavior changed.

Before, when I ran my code normally, username.sync() returned 'myuser'. But, when I ran with sudo, it returns 'root'.

In the last version, username.sync() returns 'myuser' always, independent of sudo.

Is it correct behavior? If yes, I will changed my code to use 'process.env.USER".

Thanks

Warning in console on Node 7.0.0

Just a heads up, seeing warning in console on Node 7.0.0:

(node:44604) DeprecationWarning: Using Buffer without new will soon stop working. Use new Buffer(), or preferably Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() instead.

Modules missing when trying to run

Hello,

After installing this package via npm (npm install username) and adding the required lines of code, the project cannot build anymore. If I remove the lines of code related to "username" the app build just fine. The way I implemented it is simply by writing these lines of code in ngOnInit. I might be doing it wrong... :

const username = require('username');
(async () => { console.log(await username()); })();

Here is a snippet of the console when trying to build the project :
Screen Shot 2020-03-19 at 12 26 03 AM

Thank you!

not returning username

This stopped working for me on v2.2.2.

So I checked:

  • username.sync() works for me on all versions from v.1.0.1 up to v.2.2.1
  • username.then((name)) works for me: on none of those versions.

I am using:

  • windows 10
  • node 6.2.2

returning undefined in .exe app-installer built by electron-builder

I am using username for an electron app.

Inside index.js I tried to import it:

const { username } = require('username')

That did not go well:
2023-01-25_18h35_57

So I followed vite's advice and dynamically imported:

const { username } = await import('username')
userName = await username()

That seemed to work fine, i.e. userName is alexa, as expected. In dev mode. And also: When using the .exe created in the win-unpacked folder of the release (built by electron-builder).

Here is where things get puzzling.

I then used the .exe app-installer created by electron-builder to install the app. When this app is opened, userName is undefined.

Why could this be?

was it intentional to lose support for 0.10 and 0.12?

Your package.json still says >= 0.10, but its use of const is invalid in at least <= 0.12. I tried changing const to var, and the ava tests pass, though xo enforces an unportable coding style, so it fails.

We've backdated to 1.x, but if it was intentional, updating the package.json would be helpful.

s/username (master * u=) % npm test

> [email protected] test /Users/sam/s/username
> xo && ava


  index.js:2:1
  ✖   2:1  Unexpected var, use let or const instead.  no-var
  ✖   3:1  Unexpected var, use let or const instead.  no-var
  ✖   4:1  Unexpected var, use let or const instead.  no-var
  ⚠  48:4  Unexpected todo comment.                   no-warning-comments

  3 errors
  1 warning
npm ERR! Test failed.  See above for more details.

Strangely, all those fancy unicode chars above show up as plain whitespace in my terminal... but showed up when I cut-n-pasted the whitespace into github.

ES5 support

Hi, I guess it's a conscious choice but, anyway, I'll ask, just in case. The thing is that I'm using Typescript in my Electron project and when applying the Uglify webpack plugin it fails just because it cannot understand some of the new ES2015 features (i.e. arrow functions). As your package is being published as ES2015 it's impossible for me to use it.

Would you consider applying some sort of precompilation to ES5 and publish that version to npm in order to cover almost every possible scenario? If not I will fork your repo and use my own version although I would like to use yours in order to avoid more npm noise.

Thanks for your work and best regards,
Roberto.

Question regarding security

Since this package might use the environment variables for picking up the username, it doesn't sound like this can be fully trusted (at least for Windows). The user could change the environment variables and thus spoof being someone else. Or am I missing something?

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.