Trying out Next.js and trying to create a theme directory on root I’ve been following the tailwindcss configuration but for some reason after a npm run dev
I get an error of:
Error: Cannot find module ‘@/theme/colors’
From my search found the Q&A of Cannot find module ‘next’ or its corresponding type declarations and I believe I’ve done everything suggested:
Restart Typescript
tried command + shift + p
then searched for TypeScript: Restart TS server and press enter
restarted server, same error
tsconfig.json
- ✅
"moduleResolution": "bundler",
to"moduleResolution": "node"
same error - ✅ verified path to theme directory was
"@theme/*": ["./theme/*"]
file details:
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"baseUrl": ".",
"paths": {
"@/*": ["./*"],
"@components/*": ["./components/*"],
"@theme/*": ["./theme/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
.yarnrc.yml
created the file in root .yarnrc.yml and added:
nodeLinker: node-modules
deleted the node_modules directory and re-ran:
rm -r node_modules && yarn install && npm run dev
File details
file: colors.tsx
path: root/theme/colors.tsx
contents:
type Colors = {
100: string
200: string
300: string
400: string
500: string
DEFAULT: string
600?: string
700?: string
800?: string
900?: string
}
const colorA: Colors = {
100: '#cffafe',
200: '#a5f3fc',
300: '#67e8f9',
400: '#22d3ee',
500: '#06b6d4',
DEFAULT: '#06b6d4',
600: '#0891b2',
700: '#0e7490',
800: '#155e75',
900: '#164e63',
}
const brandColors = {
colorA,
}
export default brandColors
file: tailwind.config.ts
path: root/tailwind.config.ts
import type { Config } from 'tailwindcss'
import brandColors from '@/theme/colors'
const config: Config = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
},
},
colors: {
...brandColors,
},
},
plugins: [],
}
export default config
file: package.json
contents:
"dependencies": {
"next": "14.2.3",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.3",
"postcss": "^8",
"prettier": "^3.2.5",
"tailwindcss": "^3.4.1",
"typescript": "^5"
},
"engines": {
"node": "^20.9.0"
}
Why am I getting a cannot find module in my new Next.js 14 project?