I am using __dirname
to get absolute path to GraphQL schema:
JavaScript
x
4
1
const schema = loadSchemaSync(path.join(__dirname, './graphql/schemas/schema.graphql'), {
2
loaders: [new GraphQLFileLoader()]
3
});
4
I have changed module to fit ES6 module standard and __dirname
now is undefined.
How can I resolve path to schema?
Advertisement
Answer
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 https://nodejs.org/api/esm.html#esm_import_meta_url
JavaScript
1
2
1
loadSchemaSync(path.join(import.meta.url, './graphql/schemas/schema.graphql'),
2