Node: Modules preloading suggestion

Created on 15 Feb 2020  Â·  4Comments  Â·  Source: nodejs/node

Is your feature request related to a problem? Please describe.
Not exactly a problem. Maybe something that will make life easier. Currently to require modules before executing a script we need to run node with the -r flag and it might become harder to maintain if we need to require multiple modules.

People usually use this option like a "bootstraper" or for running setup scripts (i.e. dotenv/config).

Describe the solution you'd like
I'd like to have a place to specify, in order and separated by a new line, the modules needed to be required before executing scripts. Something like:

dotenv/config
tsconfig-paths/register

And then something like that to run scripts:

node --requirements-file requirements.txt my-script.js

We could even maybe ignore the flag altogether if the requirements file -- which has yet to be named -- exists.

Describe alternatives you've considered
I'm doing something sorta like that on this repository of mine. Take a look.

cli feature request module

Most helpful comment

Thinking one step further, I think if we were to implement something like this, it should be a file that lists generic options, not just requires; e.g. node --options-file=foo where foo might contain something like

-r require1.js
-r require2.js
--pending-deprecation

… Thoughts?

All 4 comments

Just fyi, you can pass these options through an environment variable: NODE_OPTIONS=-r a.js -r b.js… does that help you?

Thanks for the help, @addaleax!

You're still needing to pass it on the command line and repeating -r before each import (and if you accidentally omit -r it will silently fail).

Personally I don't like commands to be lengthy, that's why I'm proposing this, to make it cleaner.

Thinking one step further, I think if we were to implement something like this, it should be a file that lists generic options, not just requires; e.g. node --options-file=foo where foo might contain something like

-r require1.js
-r require2.js
--pending-deprecation

… Thoughts?

That'd be nice! It'd be even nicer if we were able to provide an override mechanism such as still accept CL arguments while simultaneously passing the --options-file flag. 10/10 would use it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akdor1154 picture akdor1154  Â·  3Comments

filipesilvaa picture filipesilvaa  Â·  3Comments

vsemozhetbyt picture vsemozhetbyt  Â·  3Comments

Icemic picture Icemic  Â·  3Comments

cong88 picture cong88  Â·  3Comments