Yes, native ES modules in Node.js 16 can allow importing from the root directory using the import
statement. However, it requires configuring the package.json
file to specify the root directory as a module path. This can be done by setting the type
field in package.json
to "module"
and adding a "exports"
field that maps the root directory to the default
export of index.js
. Once configured, the root directory can be imported as a module using the following syntax:
import module from '/'
Note that importing from the root directory will only work with native ES modules, and not with CommonJS modules.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-20 05:34:00 +0000
Seen: 12 times
Last updated: May 20 '23