Importing using relative paths in Node.js with ES Modules
Asked Answered
N

2

5

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.

Neoma answered 21/7, 2018 at 4:14 Comment(7)
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.Calc
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.Neoma
If you have a specific problem, please explain it and provide stackoverflow.com/help/mcve . An answer to vague question won't necessarily help to resolve it.Calc
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.Neoma
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.Calc
npmjs.com/package/module-alias <- looks like this library module-alias works with ES Modules and does something similar.Neoma
Thanks, that's a good example. Sorry for being thorough, needed to be sure we're on the same page.Calc
C
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.

Calc answered 23/7, 2018 at 9:59 Comment(0)
M
2

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

Macadamia answered 14/8, 2019 at 9:42 Comment(1)
The path.resolve('.') in this solution is going to be whatever directory you run your script from. I found using import.meta.url as in the accepted solution was safer. I found it was necessary to add the ".js" extension as this solution does, however.Hoofed

© 2022 - 2024 — McMap. All rights reserved.