Ошибка Nx.dev: не удается найти модуль в webpackEmptyContext

Я использую https://nx.dev monorepo, подключаемый модуль узла и apollo-federation.
Я создал библиотеку с помощью интерфейса командной строки: 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)

благодаря

0 ответов

Другие вопросы по тегам