# src-to-module

> Convert source code to module

Latest version **2.0.28** (published 2021-05-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install src-to-module
pnpm add src-to-module
yarn add src-to-module
bun add src-to-module
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.28 |
| Published | 2021-05-25 |
| First published | 2020-05-27 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.22.1 |
| Dependencies | 1 |
| Unpacked size | 43.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kennys Ng |
| Maintainers | kennysng |

## Links

- npm: https://www.npmjs.com/package/src-to-module
- Repository: https://kennysng@github.com/kennysng/src-to-module
- Homepage: https://github.com/kennysng/src-to-module#readme
- Issues: https://github.com/kennysng/src-to-module/issues
- npm.io page: https://npm.io/package/src-to-module

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^4.3.2

## Recent versions

- 2.0.28 (latest) — 2021-05-25
- 2.0.27 — 2021-05-25
- 2.0.26 — 2021-05-20
- 2.0.25 — 2021-05-20
- 2.0.24 — 2021-05-20
- 2.0.23 — 2021-05-20
- 2.0.22 — 2021-05-20
- 2.0.21 — 2021-05-20
- 2.0.20 — 2021-05-20
- 2.0.19 — 2021-05-20
- 2.0.18 — 2021-05-20
- 2.0.17 — 2021-05-20
- 2.0.16 — 2021-05-06
- 2.0.15 — 2021-04-13
- 2.0.14 — 2021-04-13
- … 27 more at https://npm.io/package/src-to-module/versions

## README

# src-to-module

[![npm version](https://badge.fury.io/js/src-to-module.svg)](https://badge.fury.io/js/src-to-module)

This is a library to load NodeJS script in runtime, either in a file or in plain text. 

----

`enableLastModifiedCheck(value: boolean)`

Whether to enable last modified check on module cache. Enabled by default. If disabled, will always return cached module. 

----

`requireSync(filepath: string, context: any = {})`  
`requireAsync(filepath: string, context: any = {})`

Load modules from file.  
In case you are using `requireAsync`, there will be a `requireAsync` function in global context of the processed file.  
Note that using `await` without `async` will result to syntax error.  
Please check the [example](test/async.js). 

----

`runSync(code: string, filepath: string, context: any = {})`  
`runAsync(code: string, filepath: string, context: any = {})`

Load modules from plain text. A actual/virtual file path should be provided.  
In case this is a virtual file path, by default the cache will be expired after 30 mins if a positive `maxAge` is not provided

----

`registerTranspiler(transpiler: Transpiler)`

Register a transpier to transpile and run script.  
By default `JsTranspiler` and `JsonTranspiler` are registered. 

----

`Transpiler`

```js
interface Transpiler {
  // check from the file path if the file is valid for this transpiler
  check(path: string): boolean

  // transpile the code to, possibly, JavaScript
  transpile(path: string, code: string): string

  // run the transpiled code
  // you should use the one declared in JsTranspiler
  run<T>(path: string, code: string, require: NodeRequire, baseContext: any): T | undefined

  // run the transpiled code in async context
  // you should use the one declared in JsTranspiler
  runAsync<T>(path: string, code: string, require: NodeRequire, baseContext: any): Promise<T | undefined>
}
```

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