Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error [ERR_MODULE_NOT_FOUND]: Cannot find module

enter image description here

I'm working on a node project (screenshot). I have a single function (urls) in helpers.js which I'm exporting at the bottom as:

module.exports = {
urls: urls,
};

In my index.js I'm trying to import it with:

import { urls } from '.helpers';
const myUrls = urls(1,3);

When I run it I get

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/optionhomes11/nodeprojects/hayes/helpers' imported from /home/optionhomes11/nodeprojects/hayes/index.js Did you mean to import ../helpers.js?

What am I doing wrong?

like image 376
user1592380 Avatar asked Dec 20 '20 21:12

user1592380


People also ask

Can not find Module Express error?

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 module not found error in Javascript?

A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies. The module you're trying to import is in a different directory. The module you're trying to import has a different casing.

Can not find module path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .


Video Answer


1 Answers

When you are using ECMAScript modules you are forced to provide the file extension: https://nodejs.org/api/esm.html#esm_mandatory_file_extensions

So, on top of what other suggested of using "type": "module" on package.json you also need to specify the file extension import {urls} from './helpers.js'. You can also use the flag --es-module-specifier-resolution=node to make it resolve js files as modules just like it did before with require

like image 152
Danielo515 Avatar answered Sep 21 '22 10:09

Danielo515