inxilpro / node-app-root-path

Determine the root path to your project
MIT License
604 stars 29 forks source link

ReferenceError: __dirname is not defined in ES module scope #56

Open davay42 opened 1 year ago

davay42 commented 1 year ago

I try using the lib in my project to fix Windows builds via GitHub actions. Otherwise my projects build fine.

Seems like the lib isn't compatible with ES modules setup...

Here's the repo https://github.com/davay42/vitepress-pages

ReferenceError: __dirname is not defined in ES module scope
    at node_modules/.pnpm/app-root-path@3.1.0/node_modules/app-root-path/index.js (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vitepress-pages@0.9.0_vite-plugin-pages@0.28.0/node_modules/vitepress-pages/dist/index.js:129:26)
    at __require2 (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vitepress-pages@0.9.0_vite-plugin-pages@0.28.0/node_modules/vitepress-pages/dist/chunk-2LGAEF77.js:15:50)
    at file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vitepress-pages@0.9.0_vite-plugin-pages@0.28.0/node_modules/vitepress-pages/dist/index.js:138:36
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:541:24)
    at async loadConfigFromBundledFile (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vite@4.0.4/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:62084:21)
    at async loadConfigFromFile (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vite@4.0.4/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:61969:28)
    at async resolveConfig (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vite@4.0.4/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:61590:28)
    at async doBuild (file:///Users/davay/Documents/%D0%A4%D0%A0%D0%A3%D0%9A%D0%A2/Chromatone/chromatone.center/node_modules/.pnpm/vite@4.0.4/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:44358:20)
Creskendoll commented 9 months ago

Here's my workaround using patch-package:

diff --git a/node_modules/app-root-path/index.js b/node_modules/app-root-path/index.js
index 7fd5bde..ad84c91 100644
--- a/node_modules/app-root-path/index.js
+++ b/node_modules/app-root-path/index.js
@@ -1,4 +1,4 @@
 'use strict';

 var lib = require('./lib/app-root-path.js');
-module.exports = lib(__dirname);
\ No newline at end of file
+module.exports = lib(typeof __dirname === 'undefined' ? process.cwd() : __dirname);

Simply put this inside the file patches/app-root-path+3.1.0.patch and run npx patch-package.

Be advised that I haven't fully tested this solution, so I don't know what the implications of using process.cwd() are.