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

325
Views
NPM 7 workspaces with babel - importing between workspaces

I've stumbled across an issue while using NPM 7 workspaces and Babel where I can't wrap my head around the best way to be able to import from src/ during development but use dist/ during production.

Apologies for my awful explanation, I'll just do an example (this excludes config files and such for brevity):

My project structure is like so:

- /
  - lambdas/lambda-a
    - src/
    - package.json
  - packages/package-a
    - src/
    - package.json
  - package.json

I have my main field in packages/package-a/package.json like so:

{
  ...
  "main": "src/index.js",
  ...
}

Which means I can import stuff from packages/package-a just fine in lambdas/lambda-a/src/index.js:

import { thing } from '<package-a>'

However, by having the main field pointing to src/index.js, this isn't going to work in production, and if I change the main field to dist/index.js I can no longer import in lambdas/lambda-a during development without running a build after every change.

Does anyone have a solution for this? Or, more likely, can anyone point out where I'm being a muppet?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The workaround I use is to replace src/index.js before building and replacing it again after building/packing is done. This is neither elegant nor "how you should do it" (AFAIK), but it works for me in a small project.

I use npe because it's lightweight but there are similar (or more powerful) packages out there.

Example (packages/package-a/package.json):

...
"devDependencies": {
  "npe": "^1.1.4"
},
"scripts": {
  "build:before": "npe main dist/index.js'",
  "build": "(npm run build:before) && (if exist dist rmdir /s /q dist) && (babel src -d dist --copy-files) && (npm pack) && (npm run build:after)",
  "build:after": "npe main src/index.js",
},
...

But I'm also still searching for a more "official" way...

over 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!