Skip to content
New issue

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

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

Already on GitHub? # to your account

Having trouble with defaults - ERR_INVALID_ARG_TYPE #134

Closed
chrisandrewca opened this issue May 2, 2020 · 4 comments · Fixed by #396
Closed

Having trouble with defaults - ERR_INVALID_ARG_TYPE #134

chrisandrewca opened this issue May 2, 2020 · 4 comments · Fixed by #396

Comments

@chrisandrewca
Copy link

Hi, thanks for the slick package. I'm having some trouble getting it to work on my machine.

package.json

"scripts": {
  "db-status": "env-cmd && cd db && npx migrate-mongo status"
}

.env

MONGODB_CONN_STR=123
MONGODB_DB=ft9

Error

root@sr-dev:/app/scripts# npm run db-status

> scripts@0.0.0 db-status /app/scripts
> env-cmd && cd db && npx migrate-mongo status

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received undefined
    at validateString (internal/validators.js:117:11)
    at normalizeSpawnArguments (child_process.js:406:3)
    at Object.spawn (child_process.js:542:16)
    at Object.spawn (/app/scripts/node_modules/cross-spawn/index.js:12:24)
    at Object.EnvCmd (/app/scripts/node_modules/env-cmd/dist/env-cmd.js:59:26)
    at async Object.CLI (/app/scripts/node_modules/env-cmd/dist/env-cmd.js:19:16) {
  code: 'ERR_INVALID_ARG_TYPE'
}

invalid_arg_type

@toddbluhm
Copy link
Owner

Try:
env-cmd "cd db && npx migrate-mongo status"

(since you a replacing it inside your package.json, you will need to escape the " characters so "db-status": "env-cmd \"cd db && npx migrate-mongo status\"")

When running env-cmd you are supposed to pass the command to execute (in this case cd db && npx migrate-mongo status) as the last argument. env-cmd does not set the environment variables in the current shell, it starts a child process that has the environment variables set in that child process and executes the commands in that child process.

If the above still does not work, try env-cmd --use-shell "cd db && npx migrate-mongo status"

@chrisandrewca
Copy link
Author

Thank you so much for your help. I was able to get it to work with --use-shell. If you have a moment, could you explain how this example from basic usage works?

env-cmd mocha -R spec

Cheers,
Chris

@toddbluhm
Copy link
Owner

So in the basic usage example: env-cmd mocha -R spec

  • env-cmd by default looks for a .env file in the same folder that env-cmd is executed in (the current working directory - CWD) and uses that found .env file
  • then executes the command mocha (a NodeJS test case runner) and provides the -R spec flag/option to the mocha command

So env-cmd is executing the mocha -R spec command (with flags) in an environment where the environment variables are set to what is within the .env file + already defined system environment variables (PATH, HOME, etc...).

The reason you have to use --use-shell for your command is because you are actually executing two commands (cd and npx). So what env-cmd --use-shell does is it creates a new shell process with the .env file environment variables set in that new shell, and then executes both commands within that new shell. Without the --use-shell flag, only the cd command would receive the set environment variables.

Hopefully, that helps 😄

@chrisandrewca
Copy link
Author

That rocks thanks for the down lo. Take care.

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

Successfully merging a pull request may close this issue.

2 participants