ilearnio / module-alias

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

How to make it run in PKG ? #96

Closed fengcms closed 4 years ago

fengcms commented 4 years ago

Test project file tree:

├── config
│   └── index.js
└── index.js

Code of index.js

require('module-alias/register')
const { A, B } = require(':config')
console.log(A, B)

Code of config/index.js

module.exports = {
  A: 1,
  B: 2
}

Config in package.json

  "_moduleAliases": {
    ":config": "config"
  }

It's work in nodejs, print this:

$ node index.js
1 2

but, if I run this pkg index.js, print this:

$ pkg index.js
> pkg@4.4.8
> Targets not specified. Assuming:
  node10-linux-x64, node10-macos-x64, node10-win-x64
> Warning Cannot find module ':config' from '/Users/fungleo/Sites/Github/npm-test'
  %1: /Users/fungleo/Sites/Github/npm-test/index.js

so.... I want cry...some one help me?

Kehrlann commented 4 years ago

Hi @fengcms !

So, I won't investigate in depth, but last time someone tried to use module-alias with zeit/ncc, that was not possible. So I'm guessing it's not working for the same reason. If you want more details, see issue #81 .

However, there's a silver lining - you can configure some sort of aliases in pkg too. Not sure if you can make it work, but worth giving it a try: https://github.com/zeit/pkg#config

Not much we can do from module-alias, so I'll close this issue.