ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

not able use in node 15 with type: module in package.json #116

Open sandeshfromindia opened 3 years ago

sandeshfromindia commented 3 years ago

Hi, I am trying to use module-alias in my project and I am using node js 15.x type: module in package.json but it not working for me. see below code snippets.

package.json

{ "name": "alias-test", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "module-alias": "^2.2.2" }, "type": "module", "_moduleAliases" : { "@root" : ".", "@con" : "./config" } }

server.js

require('module-alias/register');

import '@con/module';

module.js console.log("from Module Config");

getting error :

node:internal/process/esm_loader:74 internalBinding('errors').triggerUncaughtException( ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@con/module' imported from D:\projects\Node\alias-test\server.js ?[90m at new NodeError (node:internal/errors:278:15)?[39m ?[90m at packageResolve (node:internal/modules/esm/resolve:698:9)?[39m ?[90m at moduleResolve (node:internal/modules/esm/resolve:739:18)?[39m ?[90m at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)?[39m ?[90m at Loader.resolve (node:internal/modules/esm/loader:86:40)?[39m ?[90m at Loader.getModuleJob (node:internal/modules/esm/loader:230:28)?[39m ?[90m at ModuleWrap. (node:internal/modules/esm/module_job:56:40)?[39m ?[90m at link (node:internal/modules/esm/module_job:55:36)?[39m { code: ?[32m'ERR_MODULE_NOT_FOUND'?[39m

richardeschloss commented 3 years ago

Related? https://github.com/ilearnio/module-alias/issues/59

c-DiRay commented 3 years ago

You can use node's native "imports" field, but it is less flexible then this module nodejs.org/api

package.json


{
  "imports":{
    "#root/*": "./*",
    "#con":    "./config.json"
  }
}
its-dibo commented 2 years ago

You can use node's native "imports" field, but it is less flexible then this module nodejs.org/api

package.json

{
  "imports":{
    "#root/*": "./*",
    "#con":    "./config.json"
  }
}

it forces you to rewrite all of your path aliases to be stat with '#'