env-cmd
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
Issue: I''m not able to load any .env/.js file using env-cmd
How to reproduce:
- install
npm install -g env-cmd
- 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
- 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",
same problem on mac os
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
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]
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]
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 ""
Did anyone find a fix for this?
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
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?
Yeah can't get this working on Big Sur at all 🤷♂️
Can confirm env-cmd -f
is not working on Mac OS Big Sur.
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) ¯\(ツ)/¯
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.
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
without '&&' is working fine on Windows 11. ex:
...
"scripts": {
"dev": "env-cmd -f ./.env.dev webpack serve --config config/webpack.dev.js",
@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?