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?

1 Answer 1

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 };
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for anwser, but my question was how to use extension-less import with import/export, not require/module.exports.
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' ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.