I am getting an error when trying to import a local file, though no problem when using npm packages.
import express from \'express\'
import n
It should also work if you name your module file with a .mjs
extension. Also, other ways to enable ESM are mentioned here.
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".
Strings passed in as an argument to --eval or --print, or piped to node via STDIN, with the flag --input-type=module.
on node 12 you have 2 options:
use type="module" on package.json, experimental modules and specify extensions with specidier-resolution like this:
node --experimental-modules --es-module-specifier-resolution=node src/index
or not using specifier-resolution. Keep in mind you'll have to specify the extension of your files every where.
I'm answering my own question if anybody else has this problem.
It turns out in experimental mode you need to define the full path with extension. So I am trying to import index.js
thinking it will know.
To fix it:
import express from 'express'
import next from 'next'
import api from './src/server/api/index.js'