# serverless-plugin-typescript

> [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![npm version](https://badge.fury.io/js/serverless-plugin-typescript.svg)](https://badge.fury.io/js/serverless-plugin-typescript) [![Build Status](https://travis-ci.or

Latest version **2.1.5** (published 2023-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install serverless-plugin-typescript
pnpm add serverless-plugin-typescript
yarn add serverless-plugin-typescript
bun add serverless-plugin-typescript
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.5 |
| Published | 2023-06-05 |
| First published | 2017-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.0 |
| Dependencies | 3 |
| Unpacked size | 44.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 793 |
| Maintainers | schickling, medikoo, timsuchanek, divyenduz, romanhotsiy, serverless-main, kbrandwijk |
| Keywords | serverless, serverless plugin, typescript, aws lambda |

## Links

- npm: https://www.npmjs.com/package/serverless-plugin-typescript
- Repository: https://github.com/serverless/serverless-plugin-typescript
- Homepage: https://github.com/serverless/serverless-plugin-typescript#readme
- Issues: https://github.com/serverless/serverless-plugin-typescript/issues
- npm.io page: https://npm.io/package/serverless-plugin-typescript

## Dependencies (3)

- [globby](https://npm.io/package/globby.md) ^10.0.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.1.5 (latest) — 2023-06-05
- 3.0.0-pre.82c9bc17 (pre-3) — 2021-11-24
- 2.1.4 — 2022-10-06
- 2.1.3 — 2022-10-03
- 2.1.2 — 2022-04-04
- 2.1.1 — 2022-01-28
- 2.1.0 — 2021-09-23
- 2.0.0 — 2021-09-01
- 1.2.0 — 2021-08-23
- 1.1.9 — 2019-07-15
- 1.1.8 — 2019-07-13
- 1.1.7 — 2019-04-08
- 1.1.6 — 2019-04-06
- 1.1.5 — 2017-12-29
- 1.1.4 — 2017-12-29
- … 22 more at https://npm.io/package/serverless-plugin-typescript/versions

## README

# serverless-plugin-typescript
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![npm version](https://badge.fury.io/js/serverless-plugin-typescript.svg)](https://badge.fury.io/js/serverless-plugin-typescript) [![Build Status](https://travis-ci.org/prisma/serverless-plugin-typescript.svg?branch=master)](https://travis-ci.org/prisma/serverless-plugin-typescript)

_Originally developed by [Prisma Labs](https://github.com/prisma-labs/), now maintained in scope of [Serverless, Inc](https://www.serverless.com/)_

Serverless plugin for zero-config Typescript support


## Features

* Zero-config: Works out of the box without the need to install any other compiler or plugins
* Supports ES2015 syntax + features (`export`, `import`, `async`, `await`, `Promise`, ...)
* Supports `sls package`, `sls deploy` and `sls deploy function`
* Supports `sls invoke local` + `--watch` mode
* Integrates nicely with [`serverless-offline`](https://github.com/dherault/serverless-offline)

## Install

```sh
yarn add --dev serverless-plugin-typescript typescript
# or
npm install -D serverless-plugin-typescript typescript
```

Add the following plugin to your `serverless.yml`:

```yaml
plugins:
  - serverless-plugin-typescript
```

## Configure

See [example folder](example) for a minimal example.

### `tsconfig.json`

The default `tsconfig.json` file used by the plugin looks like this:

```json
{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "rootDir": "./"
  }
}
```

> Note 1: The `outDir` and `rootDir` options cannot be overwritten.

> Note 2: Don't confuse the [`tsconfig.json`](tsconfig.json) in this repository with the one mentioned above.

### Including extra files

All files from `package/include` will be included in the final build file. See [Exclude/Include](https://serverless.com/framework/docs/providers/aws/guide/packaging#exclude--include)

### Non-standard tsconfig.json locations
Override what tsconfig.json to use with the following snippet in your severless.yaml
```
custom:
  serverlessPluginTypescript:
    tsConfigFileLocation: './tsconfig.build.json'

```

## Usage

### Google Cloud Functions

When using with Google Cloud Functions via the [serverless-google-cloudfunctions](https://github.com/serverless/serverless-google-cloudfunctions)
plugin, you simply have to provide a `main` field in your `package.json`:

```js
{
  // ...
  "main": "handler.js",
  // ..
}
```

And this plugin will automatically compile your typescript correctly. Note
that the field must refer to the compiled file name, namely, ending with a `.js`
extension.

If a `main` field was not found, then this plugin will use `index.js`. Before
compilation begins, it will check to see that the file indicated exists with a
`.ts` extension before actually trying to compile it.

### Automatic compilation

The normal Serverless deploy procedure will automatically compile with Typescript:

- Create the Serverless project with `serverless create -t aws-nodejs`
- Install Serverless Typescript as above
- Deploy with `serverless deploy`

### Usage with serverless-offline

The plugin integrates very well with [serverless-offline](https://github.com/dherault/serverless-offline) to
simulate AWS Lambda and AWS API Gateway locally.

Add the plugins to your `serverless.yml` file and make sure that `serverless-plugin-typescript`
precedes `serverless-offline` as the order is important:
```yaml
  plugins:
    ...
    - serverless-plugin-typescript
    ...
    - serverless-offline
    ...
```

Run `serverless offline` or `serverless offline start` to start the Lambda/API simulation.

In comparison to `serverless offline`, the `start` command will fire an `init` and a `end` lifecycle hook which is needed for `serverless-offline` and e.g. `serverless-dynamodb-local` to switch off resources (see below)

#### serverless-dynamodb-local

Configure your service the same as mentioned above, but additionally add the `serverless-dynamodb-local`
plugin as follows:
```yaml
  plugins:
    - serverless-plugin-typescript
    - serverless-dynamodb-local
    - serverless-offline
```

Run `serverless offline start`.

#### Other useful options

You can reduce the clutter generated by `serverless-offline` with `--dontPrintOutput` and
disable timeouts with `--noTimeout`.

### Run a function locally

To run your compiled functions locally you can:

```bash
$ serverless invoke local --function <function-name>
```

Options are:

- `--function` or `-f` (required) is the name of the function to run
- `--watch` - recompile and run a function locally on source changes
- `--path` or `-p` (optional) path to JSON or YAML file holding input data
- `--data` or `-d` (optional) input data

### Enabling source-maps

You can easily enable support for source-maps (making stacktraces easier to read) by installing and using the following plugin:

```sh
yarn add --dev source-map-support
```

```ts
// inside of your function
import 'source-map-support/register'
```

If you are using webpack (most likely). Add `devtool: 'source-map'` to `webpack.config.js`:
```js
module.exports = {
  .... snip ....
  devtool: 'source-map',
  .... snip ....

}
```

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