3

In the past, I used app-module-path whenever I wanted to have relative paths in my Node.js apps. If I use ES Modules through the .mjs format, how do I have the same functionality where a certain directory path becomes relative?

In an alternative way, would I be able to assign an alias to a directory instead so all relative paths are relative to that alias much like how ./ is an alias for a path being relative to the current directory.

Kevin Ghadyani
  • 6,154
  • 4
  • 38
  • 54
  • *I used app-module-path whenever I wanted to have relative paths in my Node.js apps* - how did you use that? It's unclear what you're asking. ES and CJS modules resolve in a similar way. – Estus Flask Jul 21 '18 at 09:48
  • They resolve in a similar way, but as soon as I import app-module-path using `import` instead of `require`, I get cryptic errors in the terminal. – Kevin Ghadyani Jul 21 '18 at 10:15
  • 1
    If you have a specific problem, please explain it and provide https://stackoverflow.com/help/mcve . An answer to vague question won't necessarily help to resolve it. – Estus Flask Jul 21 '18 at 10:17
  • I want a way to not have to use `../` anywhere in my Node.js project. `app-module-path` allows me to do this by calling `require('app-module-path/register')` in the root directory. That way I can reference all folders in the root such as `require('some-directory/a-file')` from anywhere in the project even though that file is in the root directory. There don't seem to be any solutions to do this with the import syntax. Webpack has directory aliases so I figure there's gotta be a Node.js way. – Kevin Ghadyani Jul 21 '18 at 23:37
  • *app-module-path allows me to do this by calling require('app-module-path/register') in the root directory* - how exactly? You started from that statement in the question without elaborating what this means in your case. – Estus Flask Jul 21 '18 at 23:55
  • 1
    https://www.npmjs.com/package/module-alias – Kevin Ghadyani Jul 23 '18 at 03:34
  • Thanks, that's a good example. Sorry for being thorough, needed to be sure we're on the same page. – Estus Flask Jul 23 '18 at 09:47

2 Answers2

2

It's possible to give aliases to certain paths for CommonJS modules that are loaded with require by monkey-patching built-in module module.

ES modules provide a way to change module loading behaviour by specifying custom ES module loader, as explained in this related answer.

This way root source path (which will be specified relatively to loader location) can be mapped to some alias (@ is conventional for front-end projects):

custom-loader.mjs

import path from 'path';

const ROOT_PATH = new URL(path.dirname(import.meta.url) + '/src').pathname;

export function resolve(specifier, parentModuleURL, defaultResolver) {
    specifier = specifier.replace(/^@/, ROOT_PATH);
    return defaultResolver(specifier, parentModuleURL);
}

Which is used like:

node --experimental-modules --loader ./custom-loader.mjs ./app.mjs

Notice that this provides a behaviour that isn't natural for ES modules, this may affect how this code is treated by other tools like IDEs.

Estus Flask
  • 179,509
  • 61
  • 360
  • 499
0

A more 2019 version, working for ES Modules.

The easiest way I've found so far is to use the experimental feature --loader.

I use something like that to be able to require import { SOME_CONSTANT } from '@config' or import { createConnection } from '@server/connection'.

The loader code:

import path from 'path'
import fs from 'fs'

export function resolve (specifier, parentModuleURL, defaultResolver) {
  specifier = specifier.replace(/^@/, path.resolve('.') + '/src/')
  specifier = fs.existsSync(specifier) && fs.lstatSync(specifier).isDirectory() ? `${specifier}/index` : specifier
  specifier += '.js'
  return defaultResolver(specifier, parentModuleURL)
}

Then node --experimental-modules --loader ./moduleResolver.js ./myScriptWithoutExtension

NOTE: you don't need to use .mjs extension if you specify a "type": "module" in the nearest package.json. You can stay extensionless.
NOTE2: you can replace the src string with where you usually put your code, you could even have a process.env.NODE_ENV based resolution.
NOTE3: If you give a directory to @, it will expect to find an index.js file.
NOTE4: You can use whateveer you want for aliasing, just replace the regex

user6465431354
  • 123
  • 1
  • 12