Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A sibling directory is not registered #146

Open
Nefcanto opened this issue Dec 19, 2024 · 0 comments
Open

A sibling directory is not registered #146

Nefcanto opened this issue Dec 19, 2024 · 0 comments

Comments

@Nefcanto
Copy link

Consider this structure:

Core
    Tokenize.js
    Camelize.js
App.js
package.json

Try to register the Core as an alias dynamically, in the App.js file:

import * as path from "path"
import * as url from "url"

const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
import moduleAlias from "module-alias"

const corePath = path.join(__dirname, "Core")
moduleAlias.addPath("/")
console.log(corePath)
moduleAlias.addAlias("Core", corePath)

import "module-alias/register.js"

import { camelize } from "Core"
console.log(camelize("Hello"))

You get this error:

node:internal/modules/esm/resolve:854
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'Core' imported from /HolismHolding/Generation/Generate.js
    at packageResolve (node:internal/modules/esm/resolve:854:9)
    at moduleResolve (node:internal/modules/esm/resolve:927:18)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
    at link (node:internal/modules/esm/module_job:95:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v20.18.1

This is a simple example. I'm trying to alias a lot of directories. I want to be able to write these imports:

import { camelize } from "Core"
import { getPosts } from "Blog"
import { createProduct } from "Products"
...

And each Core or Blog or ... is in a nested directory and they are dynamically added or removed (a CMS).
But even a simple setup as the above does not work.

What should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant