laf/docs/en/guide/function/depend.md
nightwhite 16cc16f61f
doc: add international English section (#1308)
* doc: add international English section

* doc: del operator.md

* doc: some old error

* doc: fix path errors

* doc: modify the default readme language

* doc: Part of the internationalization of changes

* doc: keep English readme

* doc: delete readme_en.md
2023-06-25 16:36:23 +08:00

2.6 KiB

title
Dependency Management

{{ $frontmatter.title }}

During application development, there is often a need to add npm dependencies. Laf provides an online visualization method for managing these third-party package modules, allowing users to easily search, install, upgrade, and uninstall them.

::: warning Laf cloud development can install dependencies from https://www.npmjs.com/. If the required dependencies cannot be found on this website, they cannot be installed. :::

Adding Dependencies

add-packages

As shown in the above image, we click on NPM Dependency at the bottom left of the screen, then click on the Add button, search for the package name that you want to install (in this example, we use moment), check the checkbox, and click on the Save and Restart button.

The installation time will vary depending on the size of the package and the network conditions. Please be patient and wait for it to complete.

package-list

After installation, users can view the installed dependencies and their versions in the Dependency Management section at the bottom left of the interface.

Selecting Dependency Versions

select-package-version

To ensure the stability of the user's application, Laf does not automatically update the version of the installed Npm packages.

By default, when adding a dependency, the latest version (latest) is selected. If the user wants to specify a version, they can choose the corresponding version from the version dropdown during installation.

Using in Cloud Functions

After installation, the package can be imported and used in cloud functions. For example, create a cloud function called hello-moment and modify the code as follows:

import cloud from '@lafjs/cloud'
import moment from 'moment'

export async function main(ctx: FunctionContext) {
  const momentVersion = moment.version
  const now = moment().format('YYYY-MM-DD hh:mm:ss')
  const twoHoursLater = moment().add(2, 'hours').format('YYYY-MM-DD hh:mm:ss')
  
  return { momentVersion, now, twoHoursLater}
}

Click the Run button on the right side of the interface or press Ctrl + S to save. You will see the following output:

{
  "momentVersion": "2.29.4",
  "now": "2023-02-08 02:14:05",
  "twoHoursLater": "2023-02-08 04:14:05"
}

Switching Installed Dependency Versions

change-package-version

Uninstalling installed dependencies

delete-package