As of Node.js 10.12 there’s an alternative that doesn’t require creating multiple files and handles special characters in filenames across platforms:
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
More Related Contents:
- How can I use an ES6 import in Node.js? [duplicate]
- Node.js plans to support import/export ES6 (ECMAScript 2015) modules
- module.exports vs. export default in Node.js and ES6
- Node v13 / Jest / ES6 — native support for modules without babel or esm
- ES2015 “import” not working in node v6.0.0 with with –harmony_modules option
- NodeJS require a global module/package
- How to make node.js require absolute? (instead of relative)
- Difference between app.use and app.get in express.js
- Mongoose, update values in array of objects
- How, in general, does Node.js handle 10,000 concurrent requests?
- Node.js – Maximum call stack size exceeded
- Firebase cloud functions is very slow
- MongoDB/Mongoose querying at a specific date?
- Beanstalk: Node.js deployment – node-gyp fails due to permission denied
- Node Sass does not yet support your current environment: Linux 64-bit with false
- socket.io rooms or namespacing?
- Enable Access-Control-Allow-Origin for multiple domains in Node.js [duplicate]
- Use docker run command to pass arguments to CMD in Dockerfile
- MongooseError – Operation `users.findOne()` buffering timed out after 10000ms
- Microsoft Bot framework: Sending Message on connect
- npm install -g less does not work: EACCES: permission denied
- How does require work with new operator in node.js?
- Mongoose nested query on Model by field of its referenced model
- Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (88)
- ENOENT: no such file or directory .?
- Appropriate hashbang for Node.js scripts
- Can’t install any package with node npm
- What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?
- ESLint error while trying to deploy firebase functions
- Getting 404 when attempting to publish new package to NPM