Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
Node.js Babel build and npm module-alias

I'm using module-alias for my Node.js + Express.js project, running it with Babel for ES2015 support.

App works perfect when started with babel-node, however, if I first build it with babel (from package.json):

"build": "babel ./app --out-dir ./app_dist"

And then start:

"start": "node ./app_dist/bin/www"

It obviously cannot find the correct path specified with module-alias. Instead of looking into app_dist, Node.js searches for import in app, finds ES2015 import directive which it does not understand and throws:

SyntaxError: Unexpected token import

If I change aliases before start of that build, from app to app_dist, it works, but the question is, how to map those aliases (or how to use different _moduleAliases configuration) so that app resolves the paths correctly on development and production?

Maybe there is another way to alias modules with such stack? Thanks in advance.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Found solution for this issue.

In order to set the relative path aliases with module-alias, they shoul be defined not in package.json, but in JavaScript file inside the root directory that will be transpiled with Babel.

In my case, creating an aliases.js script inside config directory like this:

import path from 'path';
import moduleAlias from 'module-alias';

moduleAlias.addAlias('@root', path.resolve(__dirname, '../../'));

Will result in path resolving relatively to the current working directory, solving described problem.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!