Open nikolasburk opened 4 years ago
same problem
I had the same problem. I was able to get both nexus and nextjs working together by adding an additional config file that would be used by nexus. I called mine nexus.tsconfig.json
. The problem is that Nextjs will always overwrite what's in tsconfig.json
--just let nextjs do it's thing ;) You'll also need to update your package.json
.
Add new config file nexus.tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
Then update the script in your package.json
to use the new config file:
...
scripts: {
...
"generate:nexus": "ts-node --transpile-only -P nexus.tsconfig.json src/pages/api",
...
}
...
If anyone want to solve this without creating a separate tsconfig file, you can configure the root tsconfig with specific options for ts-node.
Add to tsconfig:
"ts-node": { "compilerOptions": { "esModuleInterop": true, "target": "esnext", "module": "commonjs", "strict": true }, "transpileOnly": true }
Update generate command in package.json, for example:
"generate:nexus": "ts-node pages/api"
Is there any way to solve this if I use relative imports using @
?
When I run
cross-env GENERATE=true cross-env DOTENV_CONFIG_PATH=.env.example ts-node --require dotenv/config --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts
I get an error:
Error: Cannot find module '@/server/graphql/User'
The problem is then it finds all relative imports & keeps throwing error. This is a huge bummer for me. I don't want ugly absolute urls but can't get this thing to work. Any ideas?
I did try @Kingfapa's solution but it gives:
(node:12720) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use
node --trace-warnings ...
to show where the warning was created) SyntaxError: Cannot use import statement outside a module
How do I solve it?
My tsconfig.json
looks like:
{
"compilerOptions": {
"target": "es5",
"baseUrl": "node_modules",
"paths": {
"@/*": ["../src/*"]
},
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "cypress"]
}
No worries solved it using tsconfig-paths :)
Had to change my run script to cross-env GENERATE=true cross-env DOTENV_CONFIG_PATH=.env.example ts-node -r tsconfig-paths/register --require dotenv/config --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts
& it worked.
I have an example that combines GraphQL Nexus with Next.js, you can find it here: https://github.com/prisma/prisma-examples/tree/prisma2/typescript/graphql-nextjs
Now, I'm trying to invoke the Nexus generation via:
Here is my
makeSchema
:So, I'd expect that the Nexus generation outputs the two files
nexus-typegen.ts
andschema.graphql
.However, when I run the command I see this error:
I was able to fix the error with the help of this SO question, I needed to adjust the
module
in thecompilerOptions
of mytsconfig.json
fromesnext
tocommonjs
to make this work.However, when starting my Next.js app again, it autommatically overrides that setting to
esnext
again:I'm wondering how I should configure my
tsconfig.json
to use both Nexus and Next.js together?