Registry / auth-security / netrc-parser

netrc-parser

JSON →
library3.1.6jsnpmunverified

netrc-parser is a Node.js library designed to parse, load, and save `.netrc` files, which are traditionally used by FTP clients and other utilities to store login credentials for remote machines. The package provides both synchronous and asynchronous APIs for file operations and machine access. Currently at version 3.1.6, the library appears to be in a maintenance phase with its last significant update several years ago (circa 2019), indicating a stable API. It ships with TypeScript type definitions, making it suitable for modern TypeScript projects, and differentiates itself by offering a straightforward interface for managing machine entries programmatically. While there are other `netrc` parsing solutions available, this package focuses specifically on the Node.js ecosystem, handling common patterns like `login`, `password`, and `account` fields.

npm install netrc-parser
INSTALL
IMPORT
SIG · NETRC-PARSER
N
netrc-parser
auth-securityjavascriptv3.1.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Netrc
import Netrc from 'netrc-parser';
Primary ESM/TypeScript import for the default export.
Netrc
import { default as Netrc } from 'netrc-parser';
import { Netrc } from 'netrc-parser';
Alternative ESM/TypeScript import for default export. Using `import { Netrc } from ...` will fail as it's not a named export.
Netrc
const Netrc = require('netrc-parser').default;
const Netrc = require('netrc-parser');
CommonJS import; the package exports its main class as the default export, hence requiring `.default`.
Netrc
import type Netrc from 'netrc-parser';
import { Netrc } from 'netrc-parser';
TypeScript type-only import for the default export. Direct named type import will fail.

Demonstrates loading, accessing, modifying, and saving `.netrc` entries, including basic error handling for file operations.

import Netrc from 'netrc-parser'; async function manageNetrcCredentials() { const netrc = new Netrc(); try { // Load the .netrc file from the user's home directory await netrc.load(); console.log('Netrc file loaded successfully.'); // Access credentials for a specific machine const githubMachine = netrc.machines['github.com']; if (githubMachine) { console.log(`GitHub Login: ${githubMachine.login}`); console.log(`GitHub Password: ${githubMachine.password ? '[REDACTED]' : 'N/A'}`); } else { console.log('No entry found for github.com. Adding one...'); netrc.machines['github.com'] = { login: 'mygithubuser', password: process.env.GITHUB_TOKEN ?? 'my-secret-token', account: 'mygithubaccount' }; console.log('Added a placeholder entry for github.com.'); } // Save any changes back to the .netrc file await netrc.save(); console.log('Netrc file saved with potential updates.'); } catch (error) { if (error instanceof Error) { console.error(`Failed to process netrc: ${error.message}`); if (error.message.includes('permission')) { console.error('Check file permissions (e.g., chmod 600 ~/.netrc).'); } } else { console.error('An unexpected error occurred.'); } } } manageNetrcCredentials();
Debug
Known issues
gotchaThe `.netrc` file is highly sensitive and typically stores plaintext credentials. Improper file permissions (e.g., readable by other users) can lead to security vulnerabilities. Many tools, including the standard Unix `ftp` utility and Python's `netrc` module, will refuse to parse or load `.netrc` files with insecure permissions (e.g., `NetrcParseError` on POSIX systems if not `chmod 600`). This package may not enforce these checks by default, requiring manual permission management by the user.
fix
Always ensure your `~/.netrc` file has strict permissions, typically `chmod 600 ~/.netrc` on Unix-like systems, making it readable and writable only by the file owner.
affects: >=1.0.0
gotchaThis package exports its primary `Netrc` class as a default export. In CommonJS environments, this means you must access it via `.default` after requiring the package (e.g., `require('netrc-parser').default`). Directly requiring `require('netrc-parser')` will return an object containing the default export, not the class itself, leading to instantiation errors.
fix
For CommonJS, use `const Netrc = require('netrc-parser').default;`. For ESM/TypeScript, use `import Netrc from 'netrc-parser';`.
affects: >=1.0.0
gotchaLike many `netrc` parsers, `netrc-parser` can be strict about the `.netrc` file format. Non-standard entries, unknown tokens, or incorrectly formatted lines (e.g., comments not starting a line, unexpected whitespace) can lead to parsing errors or unexpected behavior. Some `netrc` implementations may tolerate more relaxed formats, which might cause compatibility issues if files are shared between different tools.
fix
Adhere strictly to the `.netrc` file format: `machine HOST login USER password PASSWORD account ACCOUNT`. Ensure comments start with `#` at the beginning of a line. Test with a minimal `.netrc` file if encountering parsing issues.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Netrc is not a constructor
Attempting to instantiate `Netrc` in CommonJS without accessing the `.default` export, or in ESM/TypeScript with an incorrect named import.
fix
For CommonJS: `const Netrc = require('netrc-parser').default;`. For ESM/TypeScript: `import Netrc from 'netrc-parser';` or `import { default as Netrc } from 'netrc-parser';`.
Error: EACCES: permission denied, open '/home/user/.netrc'
The current user does not have sufficient read/write permissions for the `.netrc` file or the directory containing it. This is a common security measure.
fix
Adjust the file permissions for `~/.netrc` to be readable and writable only by the owner: `chmod 600 ~/.netrc`. Also, ensure the parent directory has appropriate permissions.
SyntaxError: Unexpected token 'login'
The `.netrc` file contains a malformed entry or an unexpected token, causing the parser to fail. This could be due to extra data on a `machine` line or a missing value.
fix
Inspect your `~/.netrc` file for syntax errors. Ensure each `machine` block is correctly formatted with `login`, `password`, and `account` (if present) on separate lines or correctly space-separated, and that no unexpected characters or incomplete entries exist.
Upgrade
Version history
3.1.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
netrc-parser — npm install netrc-parser · libregistry