Git Product home page Git Product logo

shell-env's Introduction

shell-env

Get environment variables from the shell

Especially useful for Electron apps as GUI apps on macOS doesn't inherit the environment variables defined in your dotfiles (.bash_profile/.zshrc/etc).

Install

$ npm install shell-env

Usage

import {shellEnv} from 'shell-env';

console.log(await shellEnv());
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}

console.log(await shellEnv('/bin/bash'));
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}

API

Note that for Bash, it reads .bash_profile, but not .bashrc.

shellEnv(shell?)

Return a promise for the environment variables.

shellEnvSync(shell?)

Returns the environment variables.

shell

Type: string
Default: User default shell

Shell to read the environment variables from.

Related

  • shell-path - Get the $PATH from the shell
  • fix-path - Fix the $PATH on macOS when run from a GUI app
  • shell-history - Get the command history of the user's shell

Maintainers

shell-env's People

Contributors

albinekb avatar bendingbender avatar fazendaaa avatar lgeiger avatar rgbkrk avatar saltfish666 avatar silverwind 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

Watchers

 avatar  avatar  avatar  avatar  avatar

shell-env's Issues

error with zsh

platform: macOS
node-v : 8.7.0
shell:zsh

if the user is using zsh,zsh will return a useless header without \n

(last-4 line)

screen shot 2018-06-18 at 9 09 15 pm

$  env
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
LC_CTYPE=UTF-8
TMPDIR=/var/folders/vm/n4myfnyx0gxc0pt2vgqgqhzc0000gn/T/
TERM_SESSION_ID=A43B3347-56E7-4DAE-ABCD-D71D2CF41455
XPC_FLAGS=0x0
NVM_DIR=/Users/apple/.nvm
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.Q48wBxmpsU/Render
TERM=xterm-256color
DISPLAY=/private/tmp/com.apple.launchd.mBmoCyYbF9/org.macosforge.xquartz:0
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.O8b6Urp7ot/Listeners
SECURITYSESSIONID=186a6
GOPATH=/Users/apple/go/src/calcproj
XPC_SERVICE_NAME=0
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=388.1.1
PWD=/Users/apple/Desktop/666git/shell-env-t
SHLVL=2
_=/usr/bin/env
M2_HOME=/Users/apple/Downloads/apache-maven-3.5.3
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
SHELL=/bin/zsh
HOME=/Users/apple
LOGNAME=apple
USER=apple
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/share/dotnet:/usr/local/go/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS
OLDPWD=/Users/apple/Desktop/666git/shell-env-t
ZSH=/Users/apple/.oh-my-zsh
PAGER=less
LESS=-R
LSCOLORS=Gxfxcxdxbxegedabagacad

follows is my test to modules execa

screen shot 2018-06-18 at 10 31 04 pm

screen shot 2018-06-18 at 10 31 21 pm

Breaks if oh-my-zsh wants to auto update

The popular oh-my-zsh plugin system thing for some reason has implemented an auto-update which prompts on startup if you want to update or not.

It will block shell-env from doing it's magic.

A fix would be to set DISABLE_AUTO_UPDATE=true before starting the shell.

Failure to load `LANG` env var

Description:
shell-env sometimes does not return all the environment variables that would be present after just starting a new terminal window.

System:

  • OS: macos 10.15.3

Related Issues:
lensapp/lens#492 and lensapp/lens#351

This issue (and PR) are to fix LANG not being loaded properly.

Specifics:
It seems that macos (in particular) looks for the LANG env var to figure out how to send data to an application when pasting text. Namely, if one copies a unicode character from a web browser (firefox is what I tested) and paste it into a text region in an electron application, then the raw bytes that presented (to the JS using Uint8Array) change if LANG=en_us.UTF-8 is subsequently added to the processes env collection.

Fallback from Interactive Mode

Hi,

I'm encountering an issue where my environment isn't captured due to a zsh plugin I'm running. I run the tmux plugin for oh-my-zsh with the ZSH_TMUX_AUTOSTART option turned on, which means that when /bin/zsh -i is run, it opens up tmux and causes issues for non-interactive processes opening it up expecting a prompt but getting a tmux instead. I've encountered this issue with my text editor also, but was able to configure my text editor not to use interactive mode, which works for me because I only start up tmux automatically in interactive mode.

I understand that in practice, people are likely to set their environments in their .zshrc, so simply turning off interactive mode would not be an acceptable solution in this case. I'm wondering if you'd be open to a PR which detects whether or not the appropriate environment from running interactive mode, and if not, running non-interactive mode to see if it can gather more environment?

shell-env couldn't get current shell?

OS: macOS 10.14.3
Shell: zsh
Application environment: ElectronJS
shell-env: 3.0.0

$ echo $SHELL
-> /bin/zsh

What I get from shell-env's sync() function is

Apple_PubSub_Socket_Render: "/private/tmp/com.apple.launchd.XXXXXMj6/Render"
HMR_HOSTNAME: "localhost"
HMR_PORT: "0"
HOME: "/Users/XXXXX"
LOGNAME: "XXXXX"
PATH: "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/node_modules/.bin:/Applications/Server.app/Contents/Frameworks/Server Helper.app/Contents/MacOS"
PWD: "/"
SHELL: "/bin/bash"
SHLVL: "1"
SSH_AUTH_SOCK: "/private/tmp/com.apple.launchd.XXXXX/Listeners"
TMPDIR: "/var/folders/rg/XXXXXX/T/"
USER: "XXXXX"
XPC_FLAGS: "0x0"
XPC_SERVICE_NAME: "0"
_: "/usr/bin/env"
__CF_USER_TEXT_ENCODING: "0x1F5:0x0:0x0"

And it seems like shell-env is recognizing that shell is /bin/bash, instead of /bin/zsh

Weird Electron Webpack Error

[0]   WARNING in ./node_modules/shell-env/node_modules/cross-spawn/index.js
[0]   Module not found: Error: Can't resolve 'spawn-sync' in '/Users/nem/code/sme/spear/sal/node_modules/shell-env/node_modules/cross-spawn'
[0]    @ ./node_modules/shell-env/node_modules/cross-spawn/index.js 32:26-47
[0]    @ ./node_modules/shell-env/node_modules/execa/index.js
[0]    @ ./node_modules/shell-env/index.js
[0]    @ ./node_modules/shell-path/index.js
[0]    @ ./node_modules/fix-path/index.js
[0]    @ ./src/main/index.js
[0]    @ multi ./node_modules/electron-webpack/out/electron-main-hmr/main-hmr ./src/main/index.js

Yet I do not see cross-spawn being used anywhere in this code base up from 0.3.0.

0.3.0 is where I am seeing this issue and cross-spawn does exist in our project dependencies but as a devDependency only. cross-spawn is also only being used by a unit test.

Anyway I was wondering if anyone had any ideas?

Mention in the documentation that it reads environment variables from an interactive login shell

On my mac, this module does not load .bashrc as the README suggests. It took me a few hours to track this down, but the -ilc in /bin/bash -ilc env (used in index.js) makes bash act like an interactive login shell, which means that it only reads from .bash_profile. I assumed (wrongly) that a script within a node GUI app would be considered a non-login shell, hence I expected it to load from .bashrc.

zsh seems to behave differently. When I run /bin/zsh -ilc env on my mac, it loads variables from .zshenv, .zprofile, and .zshrc. Weird.

Anyway, I think it would be helpful for other users to understand that, in bash, this module only loads variables from files that are sourced in an interactive login shell, ie .bash_profile.

PS: Note that this is complicated by the confusing bash/zsh situation on Mac at the moment. For instance, if your default shell is bash, but then you run /bin/zsh -ilc env it will include bash environment variables, presumably because the parent shell is bash... confusing. Also, it seems that you have to log out after running chsh -s /bin/bash (as opposed to simply closing a terminal window) to fully change the default shell. Also, just changing the "shells open with command" in Terminal preferences (as some articles suggest) doesn't work. Wow, exciting morning.

Problem running in electron

shell-env works great in development mode for electron but I'm having troubles executing it in a built electron app. Any ideas?

Uncaught Exception:
/Users/rohanpai/dev/myapp/release/darwin-x64/myapp-darwin-x64/myapp.app/Contents/Resources/app/main.js:13074
          ({"NODE_ENV":"production"}) = shellEnv.sync();
          ^
SyntaxError: Unexpected token (
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Object.<anonymous> (/Users/rohanpai/dev/myapp/release/darwin-x64/myapp-darwin-x64/myapp.app/Contents/Resources/electron.asar/browser/init.js:183:8)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)

Thanks for the great work!

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.