# filename-to-module-name

> Get the shortest name file can be required by in Node.js

Latest version **0.1.1** (published 2015-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install filename-to-module-name
pnpm add filename-to-module-name
yarn add filename-to-module-name
bun add filename-to-module-name
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2015-04-02 |
| First published | 2015-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Eugene Sharygin |
| Maintainers | eush77 |
| Keywords | module, require, filename, js, json, node, file, directory, index.js, extension |

## Links

- npm: https://www.npmjs.com/package/filename-to-module-name
- Repository: https://github.com/eush77/filename-to-module-name
- Issues: https://github.com/eush77/filename-to-module-name/issues
- npm.io page: https://npm.io/package/filename-to-module-name

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.1 (latest) — 2015-04-02
- 0.1.0 — 2015-04-02

## README

[![npm](https://nodei.co/npm/filename-to-module-name.png)](https://nodei.co/npm/filename-to-module-name/)

# filename-to-module-name

[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david]

Returns the shortest module name for the given filename.

Here is a pseudocode, effectively the inverse of [require.resolve algorithm](https://nodejs.org/docs/latest/api/modules.html#modules_all_together) used by Node:
```
1. If X is a directory,
   a. return basename(X)
   b. STOP
2. Let Y be X with ".js", ".json" or ".node" removed
3. If exists(Y), return basename(X). STOP
4. For E in [".js", ".json", ".node"],
   a. If X ends with E, return basename(Y). STOP
   b. If exists(Y + E), return basename(X). STOP
5. Return basename(X)
```

[travis]: https://travis-ci.org/eush77/filename-to-module-name
[travis-badge]: https://travis-ci.org/eush77/filename-to-module-name.svg
[david]: https://david-dm.org/eush77/filename-to-module-name
[david-badge]: https://david-dm.org/eush77/filename-to-module-name.png

## Example

```js
var moduleName = require('filename-to-module-name');

moduleName('./index.js');
//=> "index"
```

## API

### `filenameToModuleName(filename)`

Returns the module name. `filename` should be a name of an existing file or directory.

## Install

```
npm install filename-to-module-name
```

## License

MIT

---
_Source: https://npm.io/package/filename-to-module-name · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
