猪猪侠9527
猪猪侠9527
  • 发布:2019-03-01 11:03
  • 更新:2019-03-01 15:09
  • 阅读:5057

const _ = require('lodash') 导入报错

分类:uni-app

const _ = require('lodash') 导入运行就会报错
是我姿势不对么?

2019-03-01 11:03 负责人:无 分享
已邀请:

最佳回复

Trust

Trust - 少说废话

import * as _ from 'lodash';  
// var _ = require('lodash');  
console.log('_:', _.chunk)

经过测试,lodash 这个库在微信小程序平台无法直接使用。

看了一下它编译后的代码,runInContext 这个函数里面的 context 指向了小程序平台的 global 对象,而非预期的 Node 环境下的 global 对象。

可能需要自己手动改下代码,大致思路就是判定一下当前环境是否有 wx 等全局变量来区分小程序与 Node 环境。

Trust

Trust - 少说废话

什么平台,代码放哪里了,是不是通过 npm 安装的,请详细描述问题。

猪猪侠9527

猪猪侠9527 (作者)

是按照步骤,npm安装的.

错误

  • Trust

    具体报什么错?运行到浏览器试试呢。

    2019-03-01 12:10

  • 猪猪侠9527 (作者)

    VM1188:1 thirdScriptError

    sdk uncaught third Error

    Cannot read property 'prototype' of undefined

    TypeError: Cannot read property 'prototype' of undefined

    at runInContext (http://127.0.0.1:25684/appservice/common/main.js:1962:27)

    at Object.<anonymous> (http://127.0.0.1:25684/appservice/common/main.js:17621:11)

    at Object.<anonymous> (http://127.0.0.1:25684/appservice/common/main.js:17640:3)

    at Object../node_modules/lodash/lodash.js (http://127.0.0.1:25684/appservice/common/main.js:17642:30)

    at webpack_require (http://127.0.0.1:25684/appservice/common/runtime.js:98:31)

    at Object.<anonymous> (http://127.0.0.1:25684/appservice/common/main.js:472:736)

    at Object.../../../../../../Users/wangq/Desktop/kuaima/xyy-uni-app/util/apis/index.js (http://127.0.0.1:25684/appservice/common/main.js:502:30)

    at
    webpack_require
    (http://127.0.0.1:25684/appservice/common/runtime.js:98:31)

    at Object.../../../../../../Users/wangq/Desktop/kuaima/xyy-uni-app/store/index.js (http://127.0.0.1:25684/appservice/common/main.js:115:15)

    at __webpack_require__ (http://127.0.0.1:25684/appservice/common/runtime.js:98:31)

    console.error @ VM1188:1

    (anonymous) @ WAService.js:1

    (anonymous) @ WAService.js:1

    e @ appservice?t=1551415763650:3844

    window.onerror @ VM1188:1

    scriptLoaded @ appservice?t=1551415763650:3984

    script.onload @ appservice?t=1551415763650:3996

    VM1263:6

    2019-03-01 12:49

猪猪侠9527

猪猪侠9527 (作者)

const _ = require('lodash')  
const fs = require('fs')  
const path = require('path')

貌似这样根本没有正常加载模块

  • 猪猪侠9527 (作者)

    index.js:12 Uncaught TypeError: fs.readdirSync is not a function

    at mapDir (index.js:12)

    at Object.<anonymous> (index.js:30)

    at Object.../../../../../../Users/wangq/Desktop/kuaima/xyy-uni-app/util/apis/index.js (index.js:2290)

    at webpack_require (bootstrap:766)

    at fn (bootstrap:129)

    at Object.../../../../../../Users/wangq/Desktop/kuaima/xyy-uni-app/store/index.js (index.js:6)

    at
    webpack_require
    (bootstrap:766)

    at fn (bootstrap:129)

    at Object.../../../../../../Users/wangq/Desktop/kuaima/xyy-uni-app/main.js (main.js:5)

    at __webpack_require__ (bootstrap:766)

    2019-03-01 13:20

  • Trust

    回复 猪猪侠9527:这是 JavaScript 项目,不是 NodeJS 项目。。。

    2019-03-01 14:07

  • Trust

    回复 猪猪侠9527:请正确理解 NodeJS 与 npm 的关系

    2019-03-01 14:07

猪猪侠9527

猪猪侠9527 (作者)

好的 ,谢谢~~

该问题目前已经被锁定, 无法添加新回复