Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] NPM not recognized the env alias #5153

Closed
2 tasks done
cebidanesrsi opened this issue Jul 12, 2022 · 4 comments
Closed
2 tasks done

[BUG] NPM not recognized the env alias #5153

cebidanesrsi opened this issue Jul 12, 2022 · 4 comments
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release

Comments

@cebidanesrsi
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

I'm migrating Docker to Podman.

I added on my env alias:

alias docker='podman'
alias docker-compose='podman-compose'

To test the alias I ran docker-compose up --build and works correctly.

I have on my package.json:

"scripts": {
    "start": "docker-compose up --build",
    ...

When a I ran npm run start, this is the output:

❯ npm run start

> project@1.0 start
> docker-compose up --build

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I did one more test, change the docker-compose to podman-compose on package.json file and works too.

Expected Behavior

Recognize the env alias. Like happen when I run the command direct on the terminal.

Steps To Reproduce

  1. Set the podman alias over docker names.
alias docker='podman'
alias docker-compose='podman-compose'
  1. Add a script on package.json to use the docker-compose.
"scripts": {
    "start": "docker-compose up --build",
    ...
  1. Run the npm script.
npm run start

Environment

  • npm: 8.13.2
  • Node.js: v14.19.3
  • OS Name: macOS 12.4 (Monterey)
  • System Model Name: Macbook Pro M1
  • npm config:
❯ npm config ls
; node bin location = /Users/XPTO/.nvm/versions/node/v14.19.3/bin/node
; node version = v14.19.3
; npm local prefix = /Users/XPTO
; npm version = 8.13.2
; cwd = /Users/XPTO
; HOME = /Users/XPTO
; Run `npm config ls -l` to show all defaults.
@cebidanesrsi cebidanesrsi added Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release labels Jul 12, 2022
@nlf
Copy link
Contributor

nlf commented Jul 13, 2022

the problem here is that your aliases have most likely been set in your default shell (zsh in your version of macOS), and while we do pass your environment variables through to the shell we run to execute your script that shell defaults to sh.

to fix your problem, there are two changes you'll need to make

1 set your aliases in ~/.zshenv instead of ~/.zprofile or ~/.zshrc
2 configure npm to use zsh instead of the default sh

after that your package.json scripts should have access to your aliases.

since this isn't an npm bug, and rather a configuration problem, i'm going to go ahead and close this issue. please do let me know if this doesn't solve your problem though!

Footnotes

  1. the ~/.zprofile script is only read for login shells, i.e. invocations of the shell that pass -l as a flag, the ~/.zshrc script is only read for interactive shells, meaning a shell that has a tty. the ~/.zshenv file on the other hand, is read every time zsh spawns another process. this is what you need in order to get npm to access your aliases

  2. npm config set script-shell zsh should do the trick here if you don't mind changing your shell for every project. if you'd rather be more specific, you can add --location=project to that command and a .npmrc file will be created in your project and the setting will only apply to that one project

@nlf nlf closed this as completed Jul 13, 2022
@cebidanesrsi
Copy link
Author

cebidanesrsi commented Jul 13, 2022

Not solve the issue.
I'm using zsh.

I did the config using the ~/.zshenv too and the error not was solved. =/

The alias are working, just npm run not able to see the alias.

Here the commands:

❯ alias | grep docker
docker=podman
docker-compose=podman-compose
❯ 
❯ 
❯ cat ~/.zshenv
# podman-compose config
alias docker='podman'
alias docker-compose='podman-compose'%                                                                                                                                                                                    
❯ 
❯ 
❯ npm run start

> project@1.0 start
> docker-compose up --build

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
❯ 
❯ 
❯ 
❯ docker-compose up --build
['podman', '--version', '']
using podman version: 4.1.1
...
{ Here all line starting the compose }
...

In additional here the npm config ls -l showing the selected shell:

searchopts = ""
searchstaleness = 900
shell = "/bin/zsh"
shrinkwrap = true

@nlf
Copy link
Contributor

nlf commented Jul 13, 2022

you've set the shell config, but you need to set script-shell. give that a try and see if it helps

@cebidanesrsi
Copy link
Author

@nlf thx!

Worked after I set the script-shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release
Projects
None yet
Development

No branches or pull requests

2 participants