module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

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

Open sandeshfromindia opened this issue 3 years ago • 3 comments

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

sandeshfromindia avatar Mar 11 '21 14:03 sandeshfromindia

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

richardeschloss avatar Mar 16 '21 20:03 richardeschloss

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"
  }
}

c-DiRay avatar Mar 23 '21 20:03 c-DiRay

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 '#'

its-dibo avatar Jan 19 '22 09:01 its-dibo