# js file import

When importing a js file in a js file or a script tag (including renderjs, etc.), you can use a relative path and an absolute path in the following form

//Absolute path, @ points to the root directory of the project, and in cli projects @ points to the src directory
import add from '@/common/add.js';
// Relative path
import add from '../../common/add.js';

Notice

  • js files do not support importing with / at the beginning

# Supported on NPM

Uni-app supports the use of npm to install third-party packages.

This document requires developers to have a certain understanding of npm, so the basic functions of npm will not be introduced. If you do not have access to npm before, please refer to the NPM official document.

Initialize npm project

If the npm management dependency has not been used before in the project (no package.json file in the project root directory), please initialize the npm project by executing the command in the project root directory first:

npm init -y

cli project already has package.json by default. The project created by HBuilderX is not available by default and needs to be created by initialization command.

Installation dependencies

Execute the command to install npm package in the root directory of the project:

npm install packageName --save

Usage

You can use npm package after installation, and introduce npm package into js:

import package from 'packageName'
const package = require('packageName')

Notice

  • In consideration of multi-terminal compatibility, it is recommended to give priority to obtain plug-ins from the uni-app plug-in market. Downloading the library directly from npm is easy to be compatible with the H5 side only.
  • The non-H5 side does not support the use of vue components and js modules with operations such as dom, window, etc. The API used by the installed module and its dependent modules must be the existing API of uni-app (compatible with small Program API), for example: support Amap WeChat Mini Program SDK. Libraries like jQuery can only be used on the H5 side.
  • The node_modules directory must be under the root directory of the project. Whether it is a cli project or a project created by HBuilderX.
  • For the acquisition of ui library, please refer to Multi-terminal UI Library