---
title: "NPM · Nuxt Scripts"
description: "Load IIFE scripts from NPM in your Nuxt app."
canonical_url: "https://scripts.nuxt.com/scripts/v0/utility/npm"
meta:
  "og:description": "Load IIFE scripts from NPM in your Nuxt app."
  "og:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_NPM%2Cdescription_Load%2BIIFE%2Bscripts%2Bfrom%2BNPM%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Utility%2Cp_Ii9zY3JpcHRzL3YwL3V0aWxpdHkvbnBtIg%2Cs_pupkTLpfFWaTUNN7.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "NPM · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_NPM%2Cdescription_Load%2BIIFE%2Bscripts%2Bfrom%2BNPM%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Utility%2Cp_Ii9zY3JpcHRzL3YwL3V0aWxpdHkvbnBtIg%2Cs_pupkTLpfFWaTUNN7.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Utility**

# **NPM**

[Copy for LLMs](https://scripts.nuxt.com/scripts/v0/utility/npm.md)

## Background

When working with NPM files, you'd typically include them as a node\_module dependency in the `**package.json**` file. However, optimizing the script loading of these scripts can be difficult, requiring a dynamic import of the module from a separate chunk and loading it only when needed. It also slows down your build as the module needs to be transpiled.

The `**useScriptNpm**` registry script abstracts this process, allowing you to load scripts that have been exported as immediately invokable functions, with a single line of code .

In many instances it will still make more sense to include the script as a dependency in the `**package.json**` file, but for scripts that are not used often or are not critical to the application, this can be a great alternative.

To begin with we can think of using this script as an alternative to the `**useHead**` composable. You can see an example of the abstraction layers in the following code sample.

```ts
useScriptNpm({
  packageName: 'js-confetti',
  file: 'dist/js-confetti.browser.js',
  version: '0.12.0',
})
```

## useScriptNpm

The `**useScriptNpm**` composable lets you have fine-grain control over when and how NPM scripts are loaded on your site.

```ts
function useScriptNpm<T extends Record<string | symbol, any>>(_options: NpmInput) {}
```

Please follow the [**Registry Scripts**](https://scripts.nuxt.com/docs/v0/guides/registry-scripts) guide to learn more about advanced usage.

### NpmOptions

```ts
export const NpmOptions = object({
  packageName: string(),
  file: optional(string()),
  version: optional(string()),
  type: optional(string()),
})
```

### Return

To get types for the script you're loading, you'll need to augment the types of the `**useScriptNpm**` function.

```ts
interface SomeApi {
  doSomething: () => void
}
useScriptNpm<SomeApi>({
  packageName: 'some-api'
})
```

## Example

See the [**Tutorial: Load js-confetti**](https://scripts.nuxt.com/docs/v0/getting-started/confetti-tutorial) for further examples.

[Edit this page](https://github.com/nuxt/scripts/edit/0.x/docs/content/utility/npm.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/v0/utility/npm.md)

[**X Pixel** Use X Pixel in your Nuxt app.](https://scripts.nuxt.com/scripts/v0/tracking/x-pixel) 

**On this page **

- [Background](#background)
- [useScriptNpm](#usescriptnpm)
- [Example](#example)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/v0/utility/npm#webpage","@type":"WebPage","description":"Load IIFE scripts from NPM in your Nuxt app.","name":"NPM","url":"https://scripts.nuxt.com/scripts/v0/utility/npm","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/v0/utility/npm#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/v0/utility/npm"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/v0/utility/npm#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"V0","item":"https://scripts.nuxt.com/scripts/v0","position":3},{"@type":"ListItem","name":"Utility","item":"https://scripts.nuxt.com/scripts/v0/utility","position":4},{"@type":"ListItem","name":"NPM","item":"https://scripts.nuxt.com/scripts/v0/utility/npm","position":5}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
