Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
dirname-filename-esm
Advanced tools
__dirname
and __filename
for ES Modules on Node.jsNode.js v13.2.0 was released with support for ES Modules. The official announcement explains the overview.
In ES Modules environment, global variables such as require
, exports
, module.exports
, __filename
,
__dirname
are not available. require
, exports
, module.exports
are no longer necessary in favor
of ESM, but we still need __filename
and __dirname
which can be calculated from import.meta.url
.
To make the migration from commonjs to ES Modules smooth, dirname-filename-esm provides functions
to create __dirname
and __filename
variables easily in an ES Modules environment.
npm install --save dirname-filename-esm
Usage is very straightforward.
// In ".mjs" script or a script under "type": "module" package
import { dirname, filename } from 'dirname-filename-esm';
const __dirname = dirname(import.meta);
const __filename = filename(import.meta);
// Use __dirname and __filename like in a commonjs module
Two functions are exported.
dirname()
takes import.meta
object and returns __dirname
value of the script.filename()
takes import.meta
object and returns __filename
value of the script.These functions return an empty string if import.meta
doesn't have url
property.
This package has zero dependency.
This package offers TypeScript support. Since this package is for
ES Modules environment, ensure that module
option is set to one of the following values in your tsconfig.json
.
es2020
or laternode16
or laterFAQs
Easy __dirname and __filename alternatives for ES Modules
The npm package dirname-filename-esm receives a total of 2,375 weekly downloads. As such, dirname-filename-esm popularity was classified as popular.
We found that dirname-filename-esm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.