复现错误过程如下。
- 新建一个云函数test, index.js代码如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
var nodejieba = require("nodejieba")
// 云函数入口函数
exports.main = async (event, context) => {
var result = nodejieba.cut("南京市长江大桥")
console.log(result)
}
2. 命令行下,切换到test目录,执行命令:npm install nodejieba
npm安装成功,日志如下:
..\cloudfunctions\test>npm install nodejieba
> nodejieba@2.5.2 install ..\cloudfunctions\test\node_modules\nodejieba
> node-pre-gyp install --fallback-to-build
[nodejieba] Success: "..\cloudfunctions\test\node_modules\nodejieba\build\Release\nodejieba.node" is installed via remote
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN test@1.0.0 No description
npm WARN test@1.0.0 No repository field.
+ nodejieba@2.5.2
added 58 packages from 96 contributors and audited 58 packages in 74.521s
3 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
3.在开发工具中点击“上传并部署:云端安装依赖”
4.执行云函数test
5.此时报错,找不到模块nodejieba:
{"errorCode":-1,"errorMessage":"Runtime.ImportModuleError: Error: Cannot find module '/var/user/node_modules/nodejieba/build/Release/nodejieba.node'\n at Object.module.exports.load (/var/runtime/node10/UserFunction.js:35:13)\n at Runtime.handleOnce (/var/runtime/node10/Runtime.engine.js:97:38)\n at Timeout.setTimeout [as _onTimeout] (/var/runtime/node10/Runtime.engine.js:54:12)\n at ontimeout (timers.js:436:11)\n at tryOnTimeout (timers.js:300:5)\n at listOnTimeout (timers.js:263:5)\n at Timer.processTimers (timers.js:223:10)","statusCode":443}
请问如何才能让云函数支持npm模块nodejieba呢?
最后解决了吗?怎么解决的?
谢邀。
jieba需要重新编译,云端安装依赖可能行不通。
试试在本地安装依赖,再整体上传部署。如果这也不行,那就没办法了。