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

Not working in Windows 10: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined #149

Closed
GorvGoyl opened this issue Jun 22, 2020 · 15 comments · Fixed by #396

Comments

@GorvGoyl
Copy link

Issue: I''m not able to load any .env/.js file using env-cmd
How to reproduce:

  1. install npm install -g env-cmd
  2. create new folder and create .env file and paste below content (taken from example repo)
TEST_NAME=Default File Path Test
NODE_ENV=env-file
ENVVAR=exists
ENV_PATH=./.env
  1. Run command env-cmd in that folder
    This throws error which is mentioned below:
PS C:\Users\1gour\OneDrive\Documents\old\Github\new> env-cmd
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:112:11)
    at Object.normalize (path.js:266:5)
    at parseNonShell (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\node_modules\cross-spawn\lib\parse.js:49:31)
    at parse (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\node_modules\cross-spawn\lib\parse.js:88:37)
    at Object.spawn (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\node_modules\cross-spawn\index.js:9:20)
    at Object.EnvCmd (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\dist\env-cmd.js:59:26)
    at async Object.CLI (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\dist\env-cmd.js:19:16) {
  code: 'ERR_INVALID_ARG_TYPE'
}

I tried to install it locally instead of globally but still same issue.

Also, If I rename .env to anything else like .env.dev it denies its existence:

PS C:\Users\1gour\OneDrive\Documents\old\Github\new> env-cmd -f .env.dev
Error: Failed to find .env file at default paths: [./.env,./.env.js,./.env.json]
    at getEnvFile (C:\Users\1gour\AppData\Roaming\nvm\v12.15.0\node_modules\env-cmd\dist\get-env-vars.js:58:11)

I've also tried using async file i.e. async-env.js but still same issue Failed to find .env file

I've tried uninstalling installing again but doesn't work. I'm not sure if somebody tested this package in Windows or not but I can't make it work!

Platform: Windows 10
Teminal: Powershell core 6.2.3 (also tried with cmd but same error)
"env-cmd": "^10.1.0",

@PerlBug
Copy link

PerlBug commented Jul 17, 2020

same problem on mac os

@bastiankoetsier
Copy link

For me -f/--file is also throwing an error:

env-cmd -f .env.development 

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received undefined

macOS Catalina
Running on Node v14.0.0 with [email protected]
The file exists in the same directory.

@rediska1114
Copy link

@bastiankoetsier

I have same error when I do not specify the executable file.

env-cmd -f .env.development throwing an error, but env-cmd -f .env.development node sample.js work ok

macOS Catalina, Node v14.5.0, [email protected]

@masterprompt
Copy link

Yeah, same here. Looks like Catalina upgrade broke it. Will have to dump env-cmd from the project now :-( was great while it lasted

macOS Catalina, Node v10.18.1, [email protected]

@mthomas46
Copy link

mthomas46 commented Oct 1, 2020

so this might help but i just ran
./node_modules/.bin/env-cmd -f "./environments/.env.ci**"** yarn dev and it worked
I had to wrap the filepath in quotes ""

@aamir-gada
Copy link

Did anyone find a fix for this?

@barder60
Copy link

barder60 commented Jan 7, 2021

when you use env-cmd commands, you have to run env-cmd just before your app launch with your command.

For windows (trying to run build:dev):

  • command line : npm run build:dev

my error was this code :

"scripts": {
    "start": "react-scripts start",
    "build": "set \"INLINE_RUNTIME_CHUNK=false\" && react-scripts build",
    "build:dev": "env-cmd -f \".env.development\" && set \"INLINE_RUNTIME_CHUNK=false\" &&  react-scripts build",
  }
  

solution :
package.json

"scripts": {
    "start": "react-scripts start",
    "build": "set \"INLINE_RUNTIME_CHUNK=false\" && react-scripts build",
    "build:dev": "set \"INLINE_RUNTIME_CHUNK=false\" && env-cmd -f \".env.development\" react-scripts build",
  }

(ignore on what does INLINE_RUNTIME_CHUNK that's just for the example)

.env.development
API_URL=http://something.com

.env
API_URL=http://otherthing.com

Now in your app you can do :

const apiUrl = process.env.API_URL 

So if you call npm run build -> apiUrl will equal "http://otherthing.com" in your app
and if you call npm run build:dev -> apiUrl will equal "http://something.com" in your app

@tmclean15
Copy link

I'm having the same issue, I've tried running on both MacOS Catalina 10.15.7 and Ubuntu 20.04. I've tried multiple different versions, including all versions from 9.0.0-10.1.0, with the same result:

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received undefined
    at validateString (internal/validators.js:121:11)
    at normalizeSpawnArguments (child_process.js:411:3)
    at Object.spawn (child_process.js:551:13)
    at Object.spawn (/root/development/labnetic/node_modules/env-cmd/node_modules/cross-spawn/index.js:12:24)
    at Object.<anonymous> (/root/development/labnetic/node_modules/env-cmd/dist/env-cmd.js:57:30)
    at Generator.next (<anonymous>)
    at fulfilled (/root/development/labnetic/node_modules/env-cmd/dist/env-cmd.js:4:58) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Has this been looked into?

@ahartzog
Copy link

ahartzog commented Sep 4, 2021

Yeah can't get this working on Big Sur at all 🤷‍♂️

@tommulkins
Copy link

Can confirm env-cmd -f is not working on Mac OS Big Sur.

@ajbarry
Copy link

ajbarry commented Nov 6, 2021

I just wanted to chime in that in my case it was due to my use of end of line comments when documenting the purpose of the variables:

Bad:

# MY_VAR=foo # development
MY_VAR=bar # production

Good:

### development environment
# MY_VAR=foo
### production environment
MY_VAR=bar

Guess I expected env-cmd to parse like a shell.

Maybe others have invisibles or something trailing the variable values or CRLF is not supported? (I'm on OSX 11.6)
¯\(ツ)

@danielcn
Copy link

danielcn commented Feb 2, 2022

I just did that without '&&' on macOS Monterey and it works fine for me:

"scripts": {
    "start": "env-cmd -f .env react-scripts start",
    "start:dev": "env-cmd -f .env-development react-scripts start",
    "build:staging": "sass src/styles/scss:src/styles/css && env-cmd -f .env-staging react-scripts build",
    "build:production": "sass src/styles/scss:src/styles/css && env-cmd -f .env-production react-scripts build",
    "test": "react-scripts test",
}

Also notice if you need to use '&&' try to use it before env-cmd command.

@kristijorgji
Copy link

I can confirm this does not work env-cmd -f .env.test
.env.test exist still get error.

I also tried full path instead of realtive still does not work

@leqnam
Copy link

leqnam commented Feb 25, 2022

without '&&' is working fine on Windows 11. ex:

...
"scripts": {
    "dev": "env-cmd -f ./.env.dev webpack serve --config config/webpack.dev.js",

@bbsimonbb
Copy link

bbsimonbb commented Sep 13, 2023

@leqnam, @danielcn and @barder60 have the solution. env-cmd requires a command. You can't just run it on its own. If you do, you get the error in the title of this issue... "The "path" argument must be of type string. Received type undefined".

The fact that so many people have tried to do this should be considered a feature request. Why should env-cmd need a command. Why can't it just load environment variables then exit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.