Hi, I’m learning to use the import and export modules but they don’t work. I am running it with the “node app” command from the visual studio code console.
from app.js
export let name = "Panterx";
/* ======================== */
from main.js
import name from "./app"
console.log(name);
node doesn’t use es6 modules by default. This is how you might work it out using node’s module system:
/** app.js */
module.exports.name = 'Panterx';
/** main.js */
const { name } = require('./app');
console.log(name);
You can run es6 modules on node (assuming your version is >= 8.5), but you’ll need to use the --experimental-modules
flag and rename the files with the .mjs
extension.
/** app.mjs */
export let name = 'Panterx';
/** main.mjs */
import { name } from './app'; // use braces for non-default exports
console.log(name);
$ node --experimental-modules main.mjs
Thanks man! It helped me a lot.