1

This is a little weird. I tried to shorten my import statements with this jsconfig.json:

{
    "compilerOptions": {
        "baseUrl": "./",
        "paths": {
            "@/*": ["./*"]
        }
    }
}

Now the weird thing is that Intellisense actually recognizes this file and gives me an auto-fill to import it.

For example: ( Please Ignore the weird file structure I've been trying out different things trying to fix this )

import { PORT } from "@/env/index";

File structure is as follows: File Structure

The error that node throws is:

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@/env' imported from C:\Users\[Censored]\main.js
    at packageResolve (internal/modules/esm/resolve.js:664:9)
    at moduleResolve (internal/modules/esm/resolve.js:705:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:819:11)      
    at Loader.resolve (internal/modules/esm/loader.js:89:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:73:40)
    at link (internal/modules/esm/module_job.js:72:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

I currently have no clue how to fix this because every tutorial I've seen on the internet does the exact same.

I hope someone can help me with this.

  • Install this https://www.npmjs.com/package/dotenv and create a .env file with the necessary values – ZombieChowder Jun 11 '22 at 22:44
  • The `paths` stuff doesn't let Node automatically resolve those paths. It only works in typescript because there's a build step, but when Node is running it doesn't know about this file and just ignores it. – Evert Jun 11 '22 at 22:45
  • @ZombieChowder I am aware of that and have that installed. This is just an example that is barebones where I get the stuff from process.env check if it exists and give it back as a variable. The problem is that it doesn't work at all. – Jonas Möller Jun 11 '22 at 22:46
  • @Evert Thank you for your explanation I appreciate it. Could you tell me how I can fix this? – Jonas Möller Jun 11 '22 at 22:48
  • Personally I think it's best to not rely on these custom paths at all! But if you must I think you can do it with: https://nodejs.org/api/packages.html#packages_subpath_imports – Evert Jun 11 '22 at 22:51
  • Hi @Evert. Thanks for your reply. Why shouldn't I rely on these imports? I personally see alot of benefits in them because my other projects all have these weird import statements like "../../xyz.js". – Jonas Möller Jun 11 '22 at 22:52
  • It's just less magic. Just a personal opinion, feel free to ignore. – Evert Jun 11 '22 at 22:54

0 Answers0