digital-loukoum / esrun

Simple wrapper around esbuild to execute a Typescript file
MIT License
173 stars 11 forks source link

Imports from fs/promises are broken #40

Open cdauth opened 1 year ago

cdauth commented 1 year ago

When I have an import fs from 'fs/promises'; or similar in my code base, this gets transpiled to import fs from "../../../promises";, causing an error such as this:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/cdauth/Documents/workspace/programming/web/osm/facilmap/promises' imported from /home/cdauth/Documents/workspace/programming/web/osm/facilmap/server/node_modules/.bin/esrun-1696190329627.tmp.mjs

The problems seems to be in this line:

                code = code
                    .replace(
                        /(?:^|;)import (.*?) from "..\//gm,
                        'import $1 from "../../../',
                    )
                    .replace(/(?:^|;)import (.*?) from ".\//gm, 'import $1 from "../../');

Here, the ..\/ part in the regular expression matches the fs/. I believe to fix the issue, the ..\/ in the regular expression should become \.\.\/ and the .\/ in the other regular expression should become \.\/.

As a workaround, I can import from node:fs/promises.