Skip to content

Unable to get __dirname value

I am using __dirname to get absolute path to GraphQL schema:

const schema = loadSchemaSync(path.join(__dirname, './graphql/schemas/schema.graphql'), {
  loaders: [new GraphQLFileLoader()]

I have changed module to fit ES6 module standard and __dirname now is undefined. __dirname

How can I resolve path to schema?


There is some issue with esm + __dirname

Differences between ES modules and CommonJS

No __filename or __dirname

These CommonJS variables are not available in ES modules.

__filename and __dirname use cases can be replicated via import.meta.url.

try to fix by this example

loadSchemaSync(path.join(import.meta.url, './graphql/schemas/schema.graphql'), ...