Skip to main content
Active reading [<https://en.wikipedia.org/wiki/JavaScript> <https://en.wikipedia.org/wiki/Node.js> <https://en.wikipedia.org/wiki/Babel_(transcompiler)>]. Removed meta information (this belongs in comments).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Wasted about 3 hours.

I just wanted to use the import and export in jsJavaScript files.

Everyone says it's not possible. But, as of May 2018, it's possible to use above in plain nodeNode.js, without any modules like babelBabel, etc.

Here is a simple way to do it.

Create the below files, run, and see the output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

File myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

File index.mjs

import myFunc from "./myfile.mjs" // simplySimply using "./myfile" may not work in all resolvers myFunc(); 

run

Run

node --experimental-modules index.mjs 

output

Output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node jsNode.js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile.mjs" // simply using "./myfile" may not work in all resolvers myFunc(); 

run

node --experimental-modules index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

I just wanted to use the import and export in JavaScript files.

Everyone says it's not possible. But, as of May 2018, it's possible to use above in plain Node.js, without any modules like Babel, etc.

Here is a simple way to do it.

Create the below files, run, and see the output for yourself.

Also don't forget to see Explanation below.

File myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

File index.mjs

import myFunc from "./myfile.mjs" // Simply using "./myfile" may not work in all resolvers myFunc(); 

Run

node --experimental-modules index.mjs 

Output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of Node.js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html
didn't work for me until i added the .mjs on the import
Source Link

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile"myfile.mjs" // this issimply ourusing myfile".mjs/myfile" may not work in all resolvers myFunc(); 

run

node --experimental-modules index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile" // this is our myfile.mjs myFunc(); 

run

node --experimental-modules index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile.mjs" // simply using "./myfile" may not work in all resolvers myFunc(); 

run

node --experimental-modules index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

an extra - has come from somewhere, removed it
Source Link

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile" // this is our myfile.mjs"mjs myFunc(); 

run

node --experimental-modules  index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile.mjs" myFunc(); 

run

node --experimental-modules index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

Wasted about 3 hours.

I just wanted to use the import and export in js files.

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

Here is a simple way to do it.

Create below files, run and see output for yourself.

Also don't forget to see Explanation below.

myfile.mjs

function myFunc() { console.log("Hello from myFunc") } export default myFunc; 

index.mjs

import myFunc from "./myfile" // this is our myfile.mjs myFunc(); 

run

node --experimental-modules  index.mjs 

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental. Hello from myFunc 

Explanation:

  1. Since it is experimental modules, .js files are named .mjs files
  2. While running you will add --experimental-modules to the node index.mjs
  3. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  4. I have the current release of node js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  5. Someday in the future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  6. More on experimental features, see: https://nodejs.org/api/esm.html

Hope that helped.

 

fix code so that it works, tested on node 12, sry for the extra nbsp
Source Link
Loading
fix grammer
Source Link
Loading
improved spelling/grammer
Source Link
Loading
simpler english wording for easy understanding
Source Link
Loading
Source Link
Loading