Nx.dev错误:无法在webpackEmptyContext

问题描述

我使用nx.dev monorepo,节点插件和阿波罗联盟
我使用CLI创建了一个库:nx generate @ nrwl / node:library acl-directive
一切看起来都很不错,但是当我尝试使用require(__ dirname +“ /assets/roles.json”)文件时,
会给我控制台错误
Error: Cannot find module 'C:\Users\Desktop\monorepo\dist\apps\apollo/assets/roles.json' 问题是,当我使用polyrepo时,一切正常,但是在此monorepo中,一切都崩溃了,我不知道原因。
我的设置和代码

// libs/acl-directive 

import { SchemaDirectiveVisitor,AuthenticationError } from 'apollo-server'
import { defaultFieldResolver } from 'graphql'
import * as fs from "fs"

// worked with this approche
// const data = fs.readFileSync(__dirname + '/assets/roles.json','utf8')
// const roles = JSON.parse(data)

// not work: error
const roles = require(__dirname + '/assets/roles.json')

export class aclDirective extends SchemaDirectiveVisitor {

  // directive on FIELD_DEFinitioN
  visitFieldDeFinition(field){

    const { resolve = defaultFieldResolver } = field

    field.resolve = async (...args) => {
      
      const [,context] = args

      // get role from context.user
      const user = JSON.parse(context.user)
      const { role } = user

      // get permissions for that specific role (user,vip,admin...)
      const permissions = roles[role]

      // check if in permissions we have field.name  (users,me,...)
      if (!permissions.includes(field.name)) throw new AuthenticationError(`Can Not Access To ${field.name}`)

      // allow to access to that resolver
      return await resolve.apply(this,args)
    }
  }
}
// libs/acl-directive/tsconfig.lib.json
// also i add this two lines to apps/todo/tsconfig.app.json
// and in tsconfig.base.json
...
"resolveJsonModule": true,"esModuleInterop": true
...

完全错误

Error: Cannot find module 'C:\Users\Desktop\monorepo\dist\apps\todo/assets/roles.json'
    at webpackEmptyContext (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\libs\acl-directive\src\lib sync ^.*\assets\roles\.json$:2:1)
    at Module../libs/acl-directive/src/lib/acl-directive.ts (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\libs\acl-directive\src\lib\acl-directive.ts:9:15)
    at __webpack_require__ (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\webpack\bootstrap:19:1)
    at Module../libs/acl-directive/src/index.ts (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\libs\acl-directive\src\index.ts:1:1)
    at __webpack_require__ (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\webpack\bootstrap:19:1)
    at Module../apps/todo/src/main.ts (C:\Users\Desktop\monorepo\dist\apps\todo\main.js:335:78)
    at __webpack_require__ (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\webpack\bootstrap:19:1)
    at Object.0 (C:\Users\Desktop\monorepo\dist\apps\todo\main.js:466:18)
    at __webpack_require__ (C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\webpack\bootstrap:19:1)
    at C:\Users\Desktop\monorepo\dist\apps\todo\webpack:\webpack\bootstrap:83:1
    at Object.<anonymous> (C:\Users\Desktop\monorepo\dist\apps\todo\main.js:87:10)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)