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

Question: Loading ES6 modules --experimental-modules #19325

Closed
Legends opened this issue Mar 13, 2018 · 9 comments
Closed

Question: Loading ES6 modules --experimental-modules #19325

Legends opened this issue Mar 13, 2018 · 9 comments
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.

Comments

@Legends
Copy link

Legends commented Mar 13, 2018

  • Version: 9.7.1
  • Platform: Win 10 x64, VsCode 1.21
  • Subsystem: -

Works fine with require.
Trying to use ES6 module import format in node.js application, but getting errors:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}\\src\\index.js",
            "runtimeArgs": [
                "--experimental-modules"
            ]           
        }
    ]
}

ModulesFile.js
export class X...

export class Y ...

Index.js
import X from"../samples/ModulesFile";

Result:

node --experimental-modules --inspect-brk=44256 src\index.js
Debugger listening on ws://127.0.0.1:44256/746a3126-db49-402c-b2b7-5cfee4c55c92
(node:51108) ExperimentalWarning: The ESM module loader is experimental.

null: SyntaxError: Unexpected identifier

@devsnek
Copy link
Member

devsnek commented Mar 13, 2018

your files with es module syntax in them will need to be named with .mjs not .js

@ChALkeR
Copy link
Member

ChALkeR commented Mar 13, 2018

https://nodejs.org/api/esm.html#esm_enabling

Once this has been set, files ending with .mjs will be able to be loaded as ES Modules.

Upd: I was late by a second :-).

@ChALkeR ChALkeR added invalid Issues and PRs that are invalid. question Issues that look for answers. labels Mar 13, 2018
@Legends
Copy link
Author

Legends commented Mar 13, 2018

I changed my entry file to be *.mjs.
Now I get:

see

Error: linking error, not in local cache

@devsnek
Copy link
Member

devsnek commented Mar 13, 2018

this was fixed on node 9.8.0

@Legends
Copy link
Author

Legends commented Mar 13, 2018

ReferenceError: require is not defined

Too experimental.... switching back to require...

@Legends Legends closed this as completed Mar 13, 2018
@ChALkeR
Copy link
Member

ChALkeR commented Mar 13, 2018

@Legends require does not exist in ESM, i.e. in *.mjs files — use import there.
You can still use require in *.js files.

That is intended to be that way and does not come from the «experimental» stability level 😉.

@Legends
Copy link
Author

Legends commented Mar 13, 2018

Yeah, I had a require nested in the code...
What about the future, do we have to rename the ".js" files to ".mjs", when using ESM?
Or is this only during "experimental mode"?

@devsnek
Copy link
Member

devsnek commented Mar 13, 2018

@Legends the future isn't 100% known yet, you can follow along at https://github.com/nodejs/modules.

@cliffhall
Copy link

cliffhall commented Aug 29, 2019

https://nodejs.org/api/esm.html#esm_enabling

The --experimental-modules flag can be used to enable support for ECMAScript modules (ES modules).

Actually, the doc says you can avoid naming with .mjs if you have a package.json appropriately configured nearby:

Once enabled, Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:

Files ending in .mjs.

Files ending in .js, or extensionless files, when the nearest parent package.json file contains a top-level field "type" with a value of "module".

This worked for me at:

node --version 
v12.6.0

I just added a package.json in the same folder as my script like this:

{
  "type": "module"
}

I ran my script with the --experimental-modules flag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants