Loading ES Modules in Node.js Projects
·
1 min read
Node.js supports ES module import. You can load ES modules through import, but there are some conditions. If not met, it will still throw errors.
Note: import in Node.js differs from the browser side.
Import Local JS Modules
- Declare
type:module
in thepackage.json
of the folder where the js file is located - File extension should be
mjs
Import URL
Executing import(url) in Node throws error ERR_UNSUPPORTED_ESM_URL_SCHEME
Error code explanation:
import
with URL schemes other thanfile
anddata
is unsupported.
From this, we can know that dynamic import in Node.js doesn’t support URLs. However, for example, Deno does support URLs, but Deno doesn’t consider CommonJS and doesn’t have this historical baggage.