Skip to content

Node –experimental-modules – Error: Cannot find module

I am getting an error when trying to import a local file, though no problem when using npm packages.


import express from 'express'
import next from 'next'
import apis from './src/server/api'


export default {
  ello: 'bye',
  jamie: 'hey'

Starting app

node --experimental-modules --inspect server.js


For help, see:
(node:20153) ExperimentalWarning: The ESM module loader is experimental.
  let url = moduleWrapResolve(specifier, parentURL);
Error: Cannot find module '/var/www/' imported from /var/www/
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:59:13)
    at Loader.resolve (internal/modules/esm/loader.js:70:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
    at link (internal/modules/esm/module_job.js:42:36) {


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'