Reputation: 166166
Is it possible to import ECMAScript modules local to my project without using a .js
or .mjs
file extension using Node's native ECMAScript modules support?
In other words -- if I have a directory structure that looks like this
index.mjs
src/some-file/index.mjs
I can import some-file/index.mjs
// File: index.mjs
import {someSymbol} from './some-file/index.mjs'
but I'm unable to do either of the following
import {someSymbol} from './some-file/index'
import {someSymbol} from './some-file'
Without resorting to bundling/transpiling/etc. or publishing a package, is there any way I can import
a local javascript file without specifying the file extension? Or will local imports always have a file extension? I've tried reading through the docs on this and have found them --- ambiguous.
Upvotes: 1
Views: 2186
Reputation: 531
import
works in upper versions of node, like node 15 as listed here: https://nodejs.org/api/esm.html
Be mindful that some contents are still experimental.
You can still use const var = require()
.
Upvotes: 0
Reputation: 56
Documentation about Mandatory file extensions
You can fix this was with the esm package. This package active experimental-modules.
npm i esm
Use:
node -r esm index.js
instead of just node index.js
Upvotes: 1