-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__dirname is not defined #2907
Comments
As we can see from the stacktrace it looks like you are loading the file as a ECMAScript module (do you have |
@mrddo-com - is this issue resolved? if yes can you close the issue please? |
The issue seem to be fix in v14.13.0 |
this way it works:
1st get the __filename and then __dirname Source:https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname |
I'm using node v14.15.4 and this issue persists. The workaround from @lundibundi and @dcjayasuriya2020 does work though. |
I'm working off of node.js 14.15, and making use of
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const app = express();
//we need to change up how __dirname is used for ES6 purposes
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//now please load my static html and css files for my express app, from my /dist directory
app.use(express.static(path.join(__dirname ,'dist')));
//works... perhaps a little more than the question needed, but shows the process.
|
I think we can create a |
@dcjayasuriya2020’s answer is the right one. |
the url is now leading to the latest docs where the corresponding section has been removed. this is the url i found: |
The docs have updated slightly, the new location is https://nodejs.org/api/esm.html#esm_no_filename_or_dirname |
Uncaught ReferenceError: __dirname is not defined |
@sunmar25 The Method 1:
|
You can use it in head of entry:
|
Do not do this.
Do not do this. The point of I’ll lock this thread – @dcjayasuriya2020’s answer is the right one, for everybody who comes looking: #2907 (comment). If you have another question, open a new ticket. |
I wrote a simple program:
Error:
The following version got error:
Node latest v14.7.0
Node LTS v12.8.2
Node LTS v12.8.3
But Node version v10.15.3 works fine.
Please help.
The text was updated successfully, but these errors were encountered: