Skip to content
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

ES module access to global or process.env improvements #19529

Closed
aabfred opened this issue Mar 22, 2018 · 3 comments
Closed

ES module access to global or process.env improvements #19529

aabfred opened this issue Mar 22, 2018 · 3 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@aabfred
Copy link

aabfred commented Mar 22, 2018

  • Version: v9.8.0

  • Platform: Linux Host-002 4.14.6-300.fc27.x86_64 deps: update openssl to 1.0.1j #1 SMP Thu Dec 14 15:31:24 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

  • Subsystem: esm

  • test.msj:
    console.log( "global.TEST", global.TEST );
    console.log( "process.env.TEST", process.env.TEST );
    global.TEST2 = "yes";
    process.env.TEST2 = "yes";

  • index.mjs:
    global.TEST = "ok";
    process.env.TEST = "ok";
    import "./test";
    console.log( "global.TEST2", global.TEST2 );
    console.log( "process.env.TEST2", process.env.TEST2 );

$ node --experimental-modules ./index.mjs
global.TEST undefined
process.env.TEST undefined
global.TEST2 yes
process.env.TEST2 yes

Problem:
Imported module cannot access to global / process.env improvements while caller accesses to improvements done by imported module

Note:
Replacing import "./test" with import "./test?test=ok" doesn't provides an access to query parameters

@targos targos added the esm Issues and PRs related to the ECMAScript Modules implementation. label Mar 22, 2018
@targos
Copy link
Member

targos commented Mar 22, 2018

If I'm not mistaken, this is expected behavior. In ES modules, imported modules are executed first (from bottom to top of the graph). The place of the import statements in the source code does not change that.
The relative order imports matters though. It means that you can have a third module that changes the global and import it before test.mjs, like so:

  • config.mjs:

     global.TEST = "ok";
     process.env.TEST = "ok";
  • test.mjs:

     console.log( "global.TEST", global.TEST );
     console.log( "process.env.TEST", process.env.TEST );
     global.TEST2 = "yes";
     process.env.TEST2 = "yes";
  • index.mjs:

     import "./config";
     import "./test";
     console.log( "global.TEST2", global.TEST2 );
     console.log( "process.env.TEST2", process.env.TEST2 );

$ node --experimental-modules index.mjs

    global.TEST ok
    process.env.TEST ok
    global.TEST2 yes
    process.env.TEST2 yes

Replacing import "./test" with import "./test?test=ok" doesn't provides an access to query parameters

It is not possible in Node 9, because the necessary APIs are not there.
It will be possible in Node 10, using import.meta:

  • test.mjs:

     const url = new URL(import.meta.url);
     console.log(url.searchParams.get('test'));
  • index.mjs:

     import "./test?test=ok";

$ node --experimental-modules index.mjs

    ok

/cc @bmeck

@bmeck
Copy link
Member

bmeck commented Mar 22, 2018

the current behavior looks correct to me.

index.mjs has a dependency on test.mjs which by ECMA262 requires loading and evaluating test.mjs prior to beginning any evaluation of index.mjs. In particular imports are essentially hoisted to the top of a file. import is not imperative but being imperative was looked at in tc39/ecma262#368 .

@aabfred
Copy link
Author

aabfred commented Mar 22, 2018

Thanks for your explainations.
I didn't knew this subtlety of ECMAScript.
You can close this issue

@bmeck bmeck closed this as completed Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

3 participants