Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 lines
8.2 KiB
Markdown
Raw Permalink Normal View History

2019-08-23 14:22:47 -07:00
---
2023-06-02 21:41:36 -07:00
title_tag: "TypeScript and Node.js | Languages & SDKs"
meta_desc: Learn to use Node.js languages like JavaScript and TypeScript with Pulumi for infrastructure as code on any cloud (AWS, Azure, Google Cloud, Kubernetes, etc.).
title: TypeScript (Node.js)
h1: Pulumi & TypeScript & JavaScript (Node.js)
2023-06-08 16:15:52 -07:00
meta_image: /images/docs/meta-images/docs-meta.png
2019-08-23 14:22:47 -07:00
menu:
languages:
2023-05-18 21:08:00 -07:00
identifier: javascript
weight: 1
aliases:
- /docs/reference/javascript/
- /docs/reference/typescript/
- /docs/intro/languages/javascript/
- /docs/intro/languages/typescript/
- /javascript/
- /typescript/
2019-08-23 14:22:47 -07:00
---
Do an editorial pass over programming model docs (#2241) * Do an editorial pass over programming model docs This is an editorial pass over the programming model docs, as part of #1618. This includes rearranging ordering to minimize forward references, getting rid of anything that smells "TypeScript-only", restructuring headers so the ToC is a bit more structured, wordsmithing, fleshing out examples, and trying to simplify the presentation of content -- while also embellishing where it helps. This is not done. The Go and TypeScript pages are empty. There is still quite a number of TypeScript-only hyperlinks and terminology. * Add Vim swp files to gitignore * Use shortcodes to cut down on language-specific text/links This change adds some shortcodes for common language-specific text and associated links. For instance, outputs are Output in JS, Output<T> in TS, Output[T] in Python, Output in Go, and Output<T> in C#, and go to very different places hyperlink-wise. Because of the way our language switcher JavaScript works, we display the JS version by default until it kicks in and picks up the user's choice. This avoids awkward flashes in which all or none of the options are available or missing. I'm not done applying this to all possible codes, but at least it lays the foundation to start paying down this debt. * Fix a couple hygiene issues * Clarify conditional execution of apply * Fix input shortcode * Add shortcodes for most language-specific things This eliminates most (all?) of the language-specific texts and links in the programming model document, replaced with shortcodes. * Fix up some loose ends * Add Go language page and Get Started guides * Fix linter errors * Apply suggestions from code review Co-Authored-By: Christian Nunciato <c@nunciato.org> * Apply additional code review feedback * Use spaces, not tabs. * Use tailwind styles, not inline CSS. Co-authored-by: Christian Nunciato <christian@pulumi.com>
2020-01-21 19:34:59 -08:00
<img src="/logos/tech/logo-nodejs.png" align="right" width="150" style="padding:8px; margin-top: -64px">
Pulumi supports writing your infrastructure as code in any JavaScript language running on Node.js using any of the [Current, Active and Maintenance LTS versions](https://nodejs.org/en/about/releases/).
2019-08-23 14:22:47 -07:00
Because programs are just JavaScript, you may elect to write them in any manner you'd normally write Node.js programs.
That includes TypeScript, CoffeeScript, or Babel, in addition to your favorite tools such as build systems, linters, or
test frameworks.
2023-05-23 18:42:56 -07:00
<a class="btn btn-secondary" href="https://nodejs.org/en/download/" target="_blank" title="Install Node.js">Install Node.js</a>
2019-08-23 14:22:47 -07:00
## Pulumi Programming Model
The Pulumi programming model defines the core concepts you will use when creating infrastructure as code programs using
Pulumi. [Concepts](/docs/intro/concepts) describes these concepts
with examples available in JavaScript and TypeScript. These concepts are made available to you in the Pulumi SDK.
The Pulumi SDK is available to Node.js developers as a NPM package. To learn more, [refer to the Pulumi SDK Reference
Guide](/docs/reference/pkg/nodejs/pulumi/pulumi).
The Pulumi programming model includes a core concept of `Input` and `Output` values, which are used to track how outputs of one resource flow in as inputs to another resource. This concept is important to understand when getting started with JavaScript and Pulumi, and the [Inputs and Outputs](/docs/concepts/inputs-outputs/) documentation is recommended to get a feel for how to work with this core part of Pulumi in common cases.
## Entrypoint
Pulumi executes your program by loading the entrypoint file as a Node module: `require("index.ts")`. By default, Pulumi will load `index.ts` or `index.js`. Alternatively, if you specify `main` within your `package.json`, Pulumi will load that module instead:
```json
{
"name": "my-package",
"version": "1.0.0",
...
"main": "src/entry.ts"
}
```
{{< chooser language "javascript,typescript" >}}
Your entrypoint can either return a module object with properties for each stack output:
{{% choosable language "javascript" %}}
```javascript
// create resources
...
exports.out = myResource.output;
```
{{% /choosable %}}
{{% choosable language "typescript" %}}
```typescript
// create resources
...
export const out = myResource.output;
```
{{% /choosable %}}
Or alternatively, your entrypoint can export a top level `async` function that returns an object with members for each stack output.
Pulumi will automatically call this function and await the result:
{{% choosable language "javascript" %}}
```javascript
module.exports = async () => {
// create resources
return { out: myResource.output };
}
```
{{% /choosable %}}
{{% choosable language "typescript" %}}
```typescript
export = async () => {
// create resources
return { out: myResource.output };
}
```
{{% /choosable %}}
{{< /chooser >}}
Most Pulumi programs use the first option, but programs that need to do async work at the top level (such as calling [`getOutputValue`](/docs/reference/pkg/nodejs/pulumi/pulumi#StackReference-getOutputValue)) may find they want to use the second.
2019-08-23 14:22:47 -07:00
## TypeScript
You can elect to write Pulumi programs in TypeScript to get additional verification and tooling benefits. Pulumi supports TypeScript natively so you don't need to explicitly run `tsc` on your program before running `pulumi`.
2019-08-23 14:22:47 -07:00
If you would like full control of the TypeScript build process, you can compile ahead of time, and point your package.json main entry point at the compiled JavaScript instead. If you do this, you can disable the [automatic compilation of TypeScript files](#disabling-built-in-typescript-support).
The fastest way to get started with Pulumi in TypeScript, is to use a template:
```bash
$ mkdir myproject && cd myproject
$ pulumi new typescript
```
This will auto-generate all the basic artifacts required to use TypeScript. If you prefer, you can instead run the following manual steps.
### 1. Update package.json
Update your `package.json` to look like the following (with your own values for `name`, `version`, etc.). This
is what tells Node.js and NPM what packages you depend on, where to find your code's entry points, and so on:
```json
{
"name": "my-package",
"version": "1.0.0",
"devDependencies": {
"@types/node": "^12.0.0"
2019-08-23 14:22:47 -07:00
},
"dependencies": {
... as before ...
}
}
```
You can customize this however you'd like, such as adding test scripts, npm package dependencies, etc. For more information on `package.json`, refer to [the NPM documentation](https://docs.npmjs.com/files/package.json).
2019-08-23 14:22:47 -07:00
### 2. Install dependencies
Run `npm install` or `yarn install` to install the new development-time dependencies to your `node_modules` directory.
### 3. Create tsconfig.json
When using Pulumi's built in TypeScript support, a `tsconfig.json` file is optional. However, defining one allows your to set additional TypeScript compiler options, for example not allowing implicit returns from a function. In addition, other tools like VS Code will use these settings to give you additional warnings at development time. Any options set in your `tsconfig.json` file will be picked up by Pulumi. We recommend creating a `tsconfig.json` file with the following settings:
```json
{
"compilerOptions": {
"strict": true,
2019-08-23 14:22:47 -07:00
"outDir": "bin",
"target": "es2016",
2019-08-23 14:22:47 -07:00
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
2019-08-23 14:22:47 -07:00
},
"files": [
"index.ts"
]
}
```
You may customize this however you'd like, including the TypeScript settings that work for you. For
information on additional settings, see the [TypeScript documentation for `tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html).
Tools like VS Code will give you completion lists, live error reporting and inline documentation help.
<img src="/images/docs/reference/vscode.png" alt="Pulumi TypeScript in VS Code" width="700">
## Disabling built in TypeScript support
You can disable the built in TypeScript support by changing the `runtime` setting in `Pulumi.yaml` to look like the following:
2019-08-23 14:22:47 -07:00
```yaml
runtime:
name: nodejs
options:
typescript: false
```
## Package Management
Pulumi has official support for NPM and Yarn Classic. Pulumi does
not support Yarn Plug'n'Play.
Pulumi defaults to using NPM. However, if Pulumi detects a `yarn.lock` file
in the project root, or the environment variable `PULUMI_PREFER_YARN=true`,
then Pulumi will use Yarn instead of NPM if the executable is available in the
path.
## Package Documentation
In addition to the standard and cloud-agnostic packages the [Pulumi Registry](/registry/) houses 100+ Node.js packages.
### Standard Packages
<dl class="tabular">
<dt>Pulumi SDK</dt>
<dd><a href="/docs/reference/pkg/nodejs/pulumi/pulumi">@pulumi/pulumi</a></dd>
<dt>Pulumi Policy</dt>
<dd><a href="/docs/reference/pkg/nodejs/pulumi/policy">@pulumi/policy</a></dd>
<dt>Pulumi Terraform</dt>
<dd><a href="/docs/reference/pkg/nodejs/pulumi/terraform">@pulumi/terraform</a></dd>
</dl>
### Cloud-Agnostic Packages
<dl class="tabular">
<dt>Pulumi Cloud Framework</dt>
<dd>
<a href="/docs/reference/pkg/nodejs/pulumi/cloud">@pulumi/cloud</a>
<span class="ml-2 badge badge-preview">PREVIEW</span>
<p>A highly productive, cloud-agnostic package for container and serverless programming.</p>
</dd>
</dl>