Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

server.js

import express from 'express'
import next from 'next'

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

api.js

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

Starting app

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

Error

For help, see: https://nodejs.org/en/docs/inspector
(node:20153) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:59
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot find module '/var/www/goldendemon.hutber.com/src/server/api' imported from /var/www/goldendemon.hutber.com/server.js
    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) {
  code: 'ERR_MODULE_NOT_FOUND'
}
like image 367
Jamie Hutber Avatar asked Jun 23 '19 12:06

Jamie Hutber


People also ask

How do I resolve Cannot find module error using node js?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find module npm?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.

What is ESM in node js?

ECMAScript modules, also known as ESM, is the official standard format to package JavaScript, and fortunately Node. js supports it 🎉.

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.


3 Answers

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'
like image 95
Jamie Hutber Avatar answered Oct 10 '22 10:10

Jamie Hutber


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.

like image 3
maazadeeb Avatar answered Oct 10 '22 09:10

maazadeeb


on node 12 you have 2 options:

  1. 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

  2. or not using specifier-resolution. Keep in mind you'll have to specify the extension of your files every where.

like image 50
Mateus.S.Machado Avatar answered Oct 10 '22 10:10

Mateus.S.Machado