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

Error when using -f *.js file

Open alegmal opened this issue 4 years ago • 14 comments

What am I missing?

This is the error:

Error: Failed to find .env file at path: ./ignore/env.js
    at getEnvFile (.../xxxxx/node_modules/env-cmd/dist/get-env-vars.js:40:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

package.json script:

"scripts": {
    "start": "env-cmd -f ./ignore/env.js node ./dist/index.js"
}

env.js content:

module.exports = {
        EXTERNAL_ADD: true,
        INTERNAL_ADD: true,
        EXECUTIONS: "invisible"
}

Also tried using Promise.resolve

module.exports = Promise.resolve ({
        EXTERNAL_ADD: true,
        INTERNAL_ADD: true,
        EXECUTIONS: "invisible"
})

Tried using full path and still the same error.

OS: Mac env-cmd: "^10.1.0" Node: v10.22.1

alegmal avatar Nov 24 '20 14:11 alegmal

Hi @alegmal. I saw your comment on my question requesting help.

I believe that your environment variable file must have the .env.js extension. So for example: myvars.env.js should work. The error message your receive says the same. Namely, that a .env file could not be found.

Let me know if it works now!

marnixhoh avatar Dec 01 '20 12:12 marnixhoh

Forgot to mention I tried that but just to triple check, here is the same error with different 'at path' I get:

Error: Failed to find .env file at path: ignore/myvars.env.js
    at getEnvFile (/xxxxxxx/node_modules/env-cmd/dist/get-env-vars.js:40:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

alegmal avatar Dec 01 '20 16:12 alegmal

Now it seems the path is /ignore/env.js instead of ./ignore/env.js . Can you confirm that it also doesn't work with ./ignore/env.js?

If that also doesn't work can you try to just use "start": "node ./dist/index.js" and confirm if this works or not?

marnixhoh avatar Dec 01 '20 17:12 marnixhoh

Same error.

Error: Failed to find .env file at path: ./ignore/.env.js
    at getEnvFile (xxxxxx/node_modules/env-cmd/dist/get-env-vars.js:40:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

"start": "node ./dist/index.js" without setting variables will run but fail because of missing envars

Changing env-cmd versions didnt help either. Do you have 10.1.0?

alegmal avatar Dec 01 '20 18:12 alegmal

are you absolutely sure the path is correct? because the error seems to suggest that it simply can't find the file...

Is the path relative to the same root as ./dist/index.js?

My script looks like this "dev": "env-cmd -f ./config/dev.env.js node src/index.js". Except that instead of node I use nodemon, but that shouldn't make a difference. The file itself looks like:

module.exports = {
    KEY: 'value'
}

Yes I do use that exact same version.

marnixhoh avatar Dec 01 '20 21:12 marnixhoh

I am having the same problem, even with absolute path:

./node_modules/.bin/env-cmd  -f /home/samim/workspace/global/config/async.js node

I get

Error: Failed to find .env file at path: /home/samim/workspace/global/config/async.js

psamim avatar Dec 14 '20 05:12 psamim

@psamim I believe you need the .env.js extension. just .js won't work.

Also, I don't think absolute paths will work. I believe that npm can't look for files outside of the project root directory.

marnixhoh avatar Dec 14 '20 08:12 marnixhoh

@marnixhoh I found the problem, there was an error in the JS file.

But the issue here is the incorrect error message. It was saying it could not find the env file, but the env file was found but had errors.

psamim avatar Dec 15 '20 07:12 psamim

@psamim Ah! That's interesting. What were the exact errors in the env file? This is probably something the maintainers would like to fix.

@toddbluhm (I took the liberty of tagging you here. Thought you'd might be interested in this.)

marnixhoh avatar Dec 15 '20 13:12 marnixhoh

@marnixhoh I was requireing a module which was not installed.

psamim avatar Dec 15 '20 14:12 psamim

@psamim Ah ok thanks for sharing that. My guess would be that all errors related to the env file incorrectly get caught and trigger the "file not found error".

marnixhoh avatar Dec 17 '20 09:12 marnixhoh

I abandoned this and did something else but nice catch psamim. If this was my case, the error message could use some improvements :)

alegmal avatar Dec 17 '20 09:12 alegmal

Hey everyone, sorry for not being around lately. I have been super busy working on a new/young startup with little free time to spare as well as migrating ts-standard over to the actual standard org.

However, I have nearly completed the next major iteration of this project. It will bring a lot of simplification, it will do a lot more "auto-discovery" of env files, as well as easy expandability using different file formats (I already have support for sync/async js/json/json5/yaml/toml/mjs/.env and maybe ts as well...still investigating) and maybe even env expansion within each env file (TBD). I have been reviewing each feature request/issue in github and would like to tackle everything I can before the release.

I have already fixed the error message stuff that is being discussed here as well. I apologize for my absence, but my day job has to come first for my family's sake.

toddbluhm avatar Dec 17 '20 17:12 toddbluhm

Thanks @toddbluhm !!

marnixhoh avatar Dec 18 '20 15:12 marnixhoh