Reputation: 53139
Or maybe I'm doing something wrong?
The documentation states, that there is experimental ES6 module loader in Node.JS since version 10. I am waiting for this for a long time - loading of modules is the only thing that prevents me from smoothly using same code in browser and node.
My code:
import findNextBracket from "./BracketFinder.js";
How I start node:
node --experimental-modules ConvertToEs6Modules.js
How I get disappointed:
(node:7116) ExperimentalWarning: The ESM module loader is experimental.
D:\web\lines\ConvertToEs6Modules.js:1
(function (exports, require, module, __filename, __dirname) { import findNextBracket from "./BracketFinder.js";
^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Proxy.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at createDynamicModule (internal/modules/esm/translators.js:54:15)
at setExecutor (internal/modules/esm/create_dynamic_module.js:50:23)
Also Visual Studio hangs if you try to run such file in it, instead of showing the error above.
Upvotes: 2
Views: 993
Reputation: 12008
Your files need to have the .mjs
extension. See the docs here
Once this has been set, files ending with
.mjs
will be able to be loaded as ES Modules.
Upvotes: 4