Getting error "Cannot find module" when using import with Node.js 14.1.0
Asked Answered
I

1

6

When trying to import like this:

import { port } from './config';

I am getting this error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/USER/PROJECT/src/config'

But when I try this import it works fine:

import { port } from './config.js';

Content of config.js:

export const port = 3000;
export const hostname = localhost;

"type" is set to "module" in my package.json, using Node.js v14.1.0

So, how can I use extension-less import?

Isma answered 4/5, 2020 at 10:21 Comment(0)
S
1

Edit:

Run node with this flag by default

node --experimental-specifier-resolution=node index.js

At the end of your config.js, export your module like this:

 module.exports = { port, hostname };
Subvert answered 22/10, 2020 at 5:10 Comment(2)
Hi, thanks for anwser, but my question was how to use extension-less import with import/export, not require/module.exports.Isma
Sorry, I misunderstood you. Can you post your package.json file? I replicated a similar environment and the results worked out well for your node version. Do you mean import { port } from 'config' ?Subvert

© 2022 - 2024 — McMap. All rights reserved.