S
S
smigles2021-01-28 15:07:37
Node.js
smigles, 2021-01-28 15:07:37

How to import a JS module without specifying an extension?

In other people's code for Node.js, I often see how JS modules are imported without specifying the extension .js, for example:

import Example from './example'; // вместо './example.js'

When I try to do the same in my code, it results in a "Cannot find module" error.

How do I set up my project so that JS modules can be imported without specifying an extension?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladlen Hellsite, 2021-01-30
@smigles

In pure Node.js, when using ESM, it is mandatory to specify the extension due to following the specification. If you want to use ESM, but not depend on pure Node.js, you need to use one of several options:

  • typescript
  • Babel
  • webpack
  • Rollup
  • esm module

S
Sergey delphinpro, 2021-01-28
@delphinpro

Read documentation. It's very good in webpack.
https://webpack.js.org/configuration/resolve/#reso...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question