npm Support
npm Support
Starting with the mini-program base library version 3.0.0 or above, and Transsion developer tools 0.8.7 or above, mini-programs support installing third-party packages via npm.
This document assumes that developers have a certain understanding of npm, therefore it will not introduce the basic functions of npm. If you have not used npm before, please refer to the official npm documentation to learn more.
Using npm Packages
1. Generate a package.json File
Execute the following command in the project root directory to generate a package.json file:
npm init -y
2. Install npm Packages
Execute the following command in the project root directory to install the npm packages you need (for example, lodash):
npm install lodash
3. After Installation, the npm Package Can Be Used
Import the npm package in js:
// index.js
import _ from 'lodash'
Page({
cloneData() {
const obj = [{ a: '1' }, { b: '2' }];
const data = _.cloneDeep(obj);
console.log('data: ', data);
}
})
Additional Notes
Tips: Since the mini-program compiler does not transpile the code inside node_modules, some pure JS libraries that require further compilation are not usable.
Some Usable npm Packages
The usage of each npm package can be found in the corresponding GitHub or official example documentation; click on the npm to navigate directly.
Utility
Date and Time
Number
URL Parsing
Encoding/Encryption
Validation