Not working with native ES modules
See original GitHub issueI’m moving a project over to using node’s native ES modules (enabled with the --experimental-modules
flag). After updating my code, module-alias is no longer working. I tried adding this to the root of my app (this is the method I was using before transitioning to esm):
require('module-alias/register')
I tried changing it to:
import 'module-alias/register'
I tried requiring when starting the server:
node --experimental-modules -r module-alias/register server/app.js
The first aliased import in my app is this:
import {responseError} from '@app/lib/response'
I’m getting this error from it:
internal/modules/esm/default_resolve.js:69
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find package '@app/lib' imported from server/app.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:69:13)
at Loader.resolve (internal/modules/esm/loader.js:70:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
at link (internal/modules/esm/module_job.js:42:36)
The relevant lines in my package.json are:
"_moduleAliases": {
"@app": "./server"
},
I’m starting the app like this:
node --experimental-modules server/app.js
module-alias worked fine using CommonJS. The only change I made to the code was changing require
s to import
s.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:32
- Comments:26
Top Results From Across the Web
How to use native ES modules - How to dev
This article will present examples of ECMAScript (ES) modules—what you can achieve with them and where you will hit some limitations.
Read more >ES modules: A cartoon deep-dive - Mozilla Hacks - the Web ...
Let's take a look at what problem ES modules solve and how they are different from modules in other module systems. What problem...
Read more >require() of ES modules is not supported when importing node ...
require() of ES modules is not supported. index.ts is an ES module file as it is a .js file whose nearest parent package.json...
Read more >Native ES Modules — Ready for Prime Time? | by Gil Tayar
To create an ES module, we write code like this, which export -s a ... It's not the network problem, as we have...
Read more >ES Modules | Dev Cheatsheets - Michael Currin
As of ES6 (ES2015), JavaScript supports a native module format called ES Modules, ... on the syntax and examples of loading modules in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cullylarson Ran into the same issue.
It appears that the new esm code isn’t running the _resolveFilename which is the core of this library. Based on the docs it looks as though they are moving off of this library’s hack and onto a standard feature: https://github.com/nodejs/node/blob/master/doc/api/esm.md#experimental-loader-hooks It’s still experimental though.
I rewrote and reduced a lot based on known things within my library but this code is working for me:
Then:
node --no-warnings --experimental-modules --es-module-specifier-resolution=node --loader ./custom-loader.mjs index.js
Not yet solved natively without execution flags?