If you want to use the import
syntax of es6+
then you will either need to use .mjs
files (instead of regular .js
files), or you will need to add in a compilation/transpilation step into your pipeline.
Using .mjs
If you change the file name of your mod.js
file to mod.mjs
, then this should work:
import Schema form 'validate';
export const test;
Then in index.js
you will either have to change index.js
to index.mjs
and change the contents to:
import { test } from './mod.mjs';
..or you can keep index.js
and change the contents to:
(async () {
const { test } = await import('./mod.mjs')
})();
You can read more in this rather comprehensive article i happened across while googling: https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/
Adding a compilation step
There are many different compilers and/or bundlers to pick from, but for regular vanilla javascript I'd recommend sticking to babel.
Freecodecamp has a tutorial for how to set up babel for use with nodejs: https://www.freecodecamp.org/news/setup-babel-in-nodejs/