-
Notifications
You must be signed in to change notification settings - Fork 492
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
NODE_CONFIG_DIR not working with gulp / ES6 (doc-patch welcome) #246
Comments
If you believe you've found a bug in node-config, please add a test to the test suite which illustrates it. I'm guessing something about transpilation of ES6 to ES5 is the issue here. For example, the use of ' If you have test which illustrates a bug in node-config, please re-open this issue. |
Something is wrong with your error output. You explicitly say
But your output doesn't show that log statement, which is a clue. I suspect there is another requires('config') earlier in the execution, because based on this line:
it shows that process.env.NODE_CONFIG_DIR hasn't been set to 'gulp/config'. I have a hard time imagining this as a bug in ES5 node-config. Try setting the NODE_CONFIG_DIR environment variable as the FIRST LINE in your executable, or in your environment before running node.js. Or, consider putting configurations in the default place. If you're thinking about distributing your app, that's where people will be looking for it. |
As to the suggestion to add arguments to node-config:
Unfortunately if we added the ability to specify overrides, they would have to be consistently applied in every module that used |
thanks @lorenwest and @markstos for looking into this issue. looks like ES6 module imports are hoisted (meaning all the dependent modules will be loaded before running any code.). spec we can see this with simple example. process.env.NODE_CONFIG_DIR = 'gulp/config';
console.log(process.env.NODE_CONFIG_DIR);
//import config from 'config';
let config = require('config');
console.log('NODE_CONFIG_DIR: ' + config.util.getEnv('NODE_CONFIG_DIR')); output
failing example process.env.NODE_CONFIG_DIR = 'gulp/config';
console.log(process.env.NODE_CONFIG_DIR);
import config from 'config';
//let config = require('config');
console.log('NODE_CONFIG_DIR: ' + config.util.getEnv('NODE_CONFIG_DIR')); output
As developers are moving to ES6 , this will limit the usability of this module with ES6. it would be nice if we re open this issue and let the community aware of this limitation. |
Thank you for discovering this. It's an interesting difference with ES6, and I'm sure it's going to be the source of many headaches during the transition. It sounds like if you want to use environment variables in node-config, you actually have to use environment variables. Another option may be to store your environment variable declarations in a module, and to load that module before loading node-config. Or you could store your configurations in the default location, and circumvent this issue all together. |
I am defined those environment variables in globals.js and referring it in the beginning of the main program as you mentioned. For those who are interested in this solution , here is a reference project: https://github.com/xmlking/gulp-ng-recipes |
Glad to hear you've gotten it working! |
I suspect we'll see more of the |
I'm re-opening this issue as a reminder that our README or a wiki page should be updated to document ES6 support gotchas and syntax. I'm also curious to test if we can still force a reload with ES6-- currently you can delete This doesn't normally come up, but we use the pattern internally in the node-config test suite, and I also use it in a private test suite to run through all the configurations we have and reality-check that they can be loaded. ... continuing to think out load: That's another pattern that might be helpful to provide an example for: A sample test that can be added to an external test suite that tests all the config files. Perhaps we could even provide a method to help with that lilke "test all configs". Providing a testing function may make most sense in a sister module like |
@markstos what kind of ES6 gotchas we should list here? I can prepare PR if you can provide at least basic list of things needs to be explained (as you probably know |
This hack worked for me. in main program:
in env.js:
You don't need to do anything with the imported values in the main program, but this makes sure the environment variables get set before |
That's an excellent solution - thanks |
Indeed a neat solution. Works well in plain Node (I am using Node 20.9.0 right now), but fails when used together with Jest, i.e. for test cases. Exactly same code that works when doing I am still digging through this, figuring there has got to be a way to work around this. Sample code follow. env.js const configDir = '/home/goran/code/butler/src/config/';
console.log(`b1 ${configDir}`);
process.env.NODE_CONFIG_DIR = configDir;
process.env.NODE_ENV = 'production';
process.env.SUPPRESS_NO_CONFIG_WARNING = 'false';
export const { NODE_CONFIG_DIR } = process.env;
export const { SUPPRESS_NO_CONFIG_WARNING } = process.env;
export const { NODE_ENV } = process.env; index.js /* eslint-disable import/order */
import { NODE_CONFIG_DIR, SUPPRESS_NO_CONFIG_WARNING, NODE_ENV } from './env.js';
import config from 'config';
console.log(`a1 ${NODE_ENV}`);
console.log(`a2 ${NODE_CONFIG_DIR}`);
console.log(`a3 ${SUPPRESS_NO_CONFIG_WARNING}`);
const logLevel = config.get('Butler.logLevel');
console.log(`Log level: ${logLevel}`); index.test.js /* eslint-disable import/order */
import { NODE_CONFIG_DIR, SUPPRESS_NO_CONFIG_WARNING, NODE_ENV } from './env.js';
import config from 'config';
console.log(`a1 ${NODE_ENV}`);
console.log(`a2 ${NODE_CONFIG_DIR}`);
console.log(`a3 ${SUPPRESS_NO_CONFIG_WARNING}`);
const logLevel = config.get('Butler.logLevel');
console.log(`Log level: ${logLevel}`); Executing > node index.js
b1 /home/goran/code/butler/src/config/
a1 production
a2 /home/goran/code/butler/src/config/
a3 false
Log level: info Executing test case > npm test index.test.js
> butler@9.4.0 test
> node --experimental-vm-modules node_modules/jest/bin/jest.js index.test.js
console.error
WARNING: No configurations found in configuration directory:/home/goran/code/butler/config
at Object.<anonymous> (node_modules/config/lib/config.js:1519:11)
console.error
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
at Object.<anonymous> (node_modules/config/lib/config.js:1520:11)
console.log
b1 /home/goran/code/butler/src/config/
at src/test/routes/env.js:2:9
console.log
a1 production
at src/test/routes/index.test.js:5:9
console.log
a2 /home/goran/code/butler/src/config/
at src/test/routes/index.test.js:6:9
console.log
a3 false
at src/test/routes/index.test.js:7:9
FAIL src/test/routes/index.test.js
● Test suite failed to run
Configuration property "Butler.logLevel" is not defined
7 | console.log(`a3 ${SUPPRESS_NO_CONFIG_WARNING}`);
8 |
> 9 | const logLevel = config.get('Butler.logLevel');
| ^
10 | console.log(`Log level: ${logLevel}`);
11 |
at Config.Object.<anonymous>.Config.get (node_modules/config/lib/config.js:179:11)
at src/test/routes/index.test.js:9:25
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.334 s
Ran all test suites matching /index.test.js/i.
(node:66713) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created) |
Found a working solution for the scenario of using Jest with config, reading the config data from a custom file/directory. The previous post does it all, except that the Can be done in a "scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
},
"jest": {
"transform": {},
"setupFiles": ["./src/test/env.js"]
}, |
I am setting
NODE_CONFIG_DIR
in the beginning of mygulpfile.babel.js
file, but it seams executed afterimport
files are loaded. please advice any workaround.it would nice if we can pass settings like:
Error:
The text was updated successfully, but these errors were encountered: