nodejs打包引用错误
import R from 'ramda'
function useMiddlewares(app) {
return R.map(R.compose(
R.map(i => i(app)), // 调用中间件的方法,并且将 app 作为参数传入
require, // 将中间件引入
i => `${r('./middlewares')}/${i}` // 获取到中间件目录
)
}
useMiddlewares(['general', 'database', 'router'])
上面的代码在开发环境没有问题,但是打包之后在正式环境就会出现下面的错误:
/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:243
return !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }());
^
Error: Cannot find module "."
at webpackMissingModule (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:243:60)
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:243:18
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_pipe.js:3:14
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_pipe.js:3:27
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_arity.js:5:45
at _map (/Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_map.js:6:19)
at map (/Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/map.js:57:14)
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_dispatchable.js:39:15
at /Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_curry2.js:20:46
at f1 (/Users/xuyuechao/Work/other-git-project/nuxt-koa/node_modules/_ramda@0.23.0@ramda/src/internal/_curry1.js:17:17)
at new Server (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:228:5)
at Object.<anonymous> (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:306:11)
at Object.<anonymous> (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:308:30)
at __webpack_require__ (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:22:30)
at module.exports.module.exports.head.title (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:68:18)
at Object.<anonymous> (/Users/xuyuechao/Work/other-git-project/nuxt-koa/build/main.js:3:10)
刚开始以为是打包编译之后将问价名给修改了,所以报错,但是试验过之后发现跟这个没有关系。现在没有思路,各方高手,看看能不能够给提供点思路
补充
我又进行了一番尝试,发现上面的代码可以缩减为:
import { resolve } from 'path'
const r = path => resolve(__dirname, path)
require(r('./middlewares/general'))
错误信息不变,但是当我把上面的代码修改为:
require('usr/local/middlewares/general')
就没有问题了,也就是说使用r函数组装的路径就不可以,直接使用绝对路径就没有问题。但是为什么开发环境没有问题,正式环境就不行了呢,还是很疑惑?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)