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?
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.
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.
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' .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With