1

Premise

I have a VueJS project that uses service modules that I wrote.

Since I used vue cli I used the handy @ symbol to denote my src folder and easily load all my modules using that pattern:

  /* Inside someService.js */
  import API from '@/services/APIService.js'

Problem

The problem is that now I am trying to load these modules using nodejs to use the same functionality on the server and avoid duplication of code.

I got around the import support in node using esm but I get an error for all modules using the @ symbol.

Possible solution

I can go through my entire code base and try and get rid of the @ symbol but that would be my last resort.

Question

How do I load these modules using the @ symbol to denote the src folder?

hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
  • 1
    This npm package, [module-alias](https://www.npmjs.com/package/module-alias) might help – Stephen S Jul 02 '19 at 05:28
  • @StephenS Looks like what I need but it's not working. Have you used this before? – hitautodestruct Jul 02 '19 at 05:55
  • Unfortunately I haven't, I just searched it out of curiosity ! – Stephen S Jul 02 '19 at 06:04
  • See what you tried with this module-alias, maybe I can help you. precisely your package.json – BENARD Patrick Jul 02 '19 at 06:15
  • I think that you should use webpack for node. In this case you will have two builds, one for web and another for node. Here some advantages(e.g. babel) and disadvantages(e.g. you have to use source maps for testing). Aliases it is not part of web or nodejs, it is feature of build managers, such as webpack. link: https://webpack.js.org/concepts/targets/ – Alexandr Vysotsky Jul 02 '19 at 06:25
  • @StephenS pimento-web utlime Thanks for your references and help for pointing me in the right direction! – hitautodestruct Jul 03 '19 at 06:44

1 Answers1

1

Answer

I ended up using link-module-alias.

It creates a symlink for your alias and just like module-alias its defined in the same way inside package.json you add:

{
  // ... details, dependencies etc.
  "_moduleAliases": {
    "@": "./src"
  },
}

Possible answer

I saw that a possible solution exists using module-alias. You would defined the exact same "_moduleAliases" as in the answer above in your package.json but instead of a symlink you would use module-alias at runtime like so:

node -r module-alias/register -r esm index.js 8081

But it did not work for me.

hitautodestruct
  • 20,081
  • 13
  • 69
  • 93