module-id
Version:
Parses commonjs module id into name, version and path.
79 lines (54 loc) • 2.05 kB
Markdown
[](https://travis-ci.org/kaelzhang/node-module-id)
[](https://codecov.io/gh/kaelzhang/node-module-id)
<!-- optional appveyor tst
[](https://ci.appveyor.com/project/kaelzhang/node-module-id)
-->
<!-- optional npm version
[](http://badge.fury.io/js/module-id)
-->
<!-- optional npm downloads
[](https://www.npmjs.org/package/module-id)
-->
<!-- optional dependency status
[](https://david-dm.org/kaelzhang/node-module-id)
-->
# module-id
Parse commonjs module id into `name`, `version` and `path`.
## Install
```bash
$ npm i module-id
```
## id(module_id)
```js
const id = require('module-id')
const parsed = id('a/abc.js')
parsed.name // 'a',
parsed.version // undefined,
parsed.path // '/abc.js'
parsed.id // 'a/abc.js'
parsed.url // 'a/*/abc.js', the normalized url path
parsed.pkg // 'a@*'
// Change version
parsed.version = '1.1.0'
parsed.id // 'a@1.1.0/abc.js'
```
## About Scoped Packages
### Getter: .name
The name of a package, i.e. the `name` field of package.json, supports [scoped package](https://docs.npmjs.com/misc/scope) names
Setting `scope` will affect the value of `name`
```js
const parsed = id('a/index.js')
parsed.scope // undefined
parsed.scope = 'facebook'
parsed.name // '@facebook/a'
parsed.id // '@facebook/a/index.js'
```
### Setter: .name
If the given value of `name` is a scoped package name, it will also affect the `scope` property
```js
const parsed = id('a/index.js')
parsed.name = '@facebook/a'
parsed.scope // 'facebook'
```
## License
[MIT](LICENSE)