env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

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

Open GorvGoyl opened this issue 4 years ago • 15 comments

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",

GorvGoyl avatar Jun 22 '20 22:06 GorvGoyl

same problem on mac os

PerlBug avatar Jul 17 '20 03:07 PerlBug

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.

bastiankoetsier avatar Aug 17 '20 12:08 bastiankoetsier

@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]

rediska1114 avatar Aug 19 '20 12:08 rediska1114

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]

masterprompt avatar Aug 30 '20 21:08 masterprompt

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 ""

mthomas46 avatar Oct 01 '20 21:10 mthomas46

Did anyone find a fix for this?

aamir-gada avatar Dec 05 '20 15:12 aamir-gada

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

barder60 avatar Jan 07 '21 17:01 barder60

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?

tmclean15 avatar Apr 29 '21 12:04 tmclean15

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

ahartzog avatar Sep 04 '21 10:09 ahartzog

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

tommulkins avatar Oct 12 '21 21:10 tommulkins

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) ¯\(ツ)

ajbarry avatar Nov 06 '21 03:11 ajbarry

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.

danielcn avatar Feb 02 '22 11:02 danielcn

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

kristijorgji avatar Feb 03 '22 18:02 kristijorgji

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

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

leqnam avatar Feb 25 '22 10:02 leqnam

@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?

bbsimonbb avatar Sep 13 '23 10:09 bbsimonbb