2020-11-28

Current working directory problem with ESM

Imagine you have a Node.js express server and your structure looks like this:

app.mjs

services
-utils
--test.mjs


website
-js
--web.mjs
--req.mjs
-index.html

web.mjs:

import * as req from '/js/req.mjs';
export const sample = ()=>{/*..*/};
export const sampletwo = ()=>{/*..*/};

index.html:

<script type="module" src="js/web.mjs"></script>

This path works fine because the working directory comes from the index.html file when we open it in our browser. However, when I want to read the web.mjs file Server Side then I get error Cannot find module '/js/req.mjs' because the working directory is different now.

test.mjs:

import * as web from '../../website/js/web.mjs';

Is there a way to set the current working directory in ESM? Like you use CD in terminal. So that always when I try to read web.mjs it will use the working directory where the file itself is located?



from Recent Questions - Stack Overflow https://ift.tt/33mC0yL
https://ift.tt/eA8V8J

No comments:

Post a Comment