TypeScript rejects createRoot(document.getElementById('root'))
Argument of type 'HTMLElement | null' is not assignable to parameter of typeShort answer
document.getElementById returns an element or null, and createRoot only accepts a real DOM element. With strictNullChecks on, TypeScript refuses the call. Check for null before the call, throw a clear error if the root is missing, or use the ! assertion only when the HTML file really has that id.
Fix it fast
1Find out which cause you have
Look at the line with createRoot in your entry file, and check that the HTML page really has an element with that id.
grep -n "createRoot\|getElementById" src/main.tsx grep -n 'id="root"' index.html2Match what you see, then jump to its fix
- 1
TS2345 on the createRoot line, while the app runs fine in the browser.strictNullChecks is on and the null case is not handledFix - 2
The null check above hides a wrong id: the app compiles and then shows nothing.You want a loud failure instead of a silent blank pageFix - 3
The guard throws at runtime although the id matches.The script runs before the root element existsFix
- 1
What does the error look like?
Argument of type 'HTMLElement | null' is not assignable to parameter of typeType 'null' is not assignable to type ... ts(2345)Type 'null' is not assignable to type 'Element | DocumentFragment'Target container is not a DOM element
Cause 1 of 3
strictNullChecks is on and the null case is not handled
1Check
getElementById is documented to return null when no element matches. With strictNullChecks on, TypeScript requires you to test for null before using the value, so the call is a type error even if the element exists at runtime.
grep -n strict tsconfig*.json2Fix
Narrow the value before the call.
const container = document.getElementById('root');
if (container) {
createRoot(container).render(<App />);
}3Verify
The type checker passes.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
You want a loud failure instead of a silent blank page
1Check
The lookup is case-sensitive, and returns null when the element is not in the document. A typo in the id, or a different id in index.html, silently skips the render.
grep -n 'id=' index.html2Fix
Throw when the root is missing, so the real problem shows up at once.
const container = document.getElementById('root');
if (!container) throw new Error('Root element #root not found');
createRoot(container).render(<App />);3Verify
Rename the id in index.html on purpose and see your own error message; then restore it.
npm run devConfirmed by developer.mozilla.org
Cause 3 of 3
The script runs before the root element exists
1Check
React's createRoot reference says the bundle's script tag cannot see DOM nodes that appear after it in the HTML, and the container is then null.
grep -n 'script\|id="root"' index.html2Fix
Place the script tag after the element, or run the code once the DOM is ready. Use the ! assertion only when you know the element exists: it changes the type, not the runtime behaviour.
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>3Verify
The page renders and the console shows no error.
npm run buildConfirmed by react.dev
How do I stop it coming back?
- Keep strictNullChecks on and handle the null once, in the entry file.
- Use a guarded throw rather than the ! assertion for the root element.
- Keep the id in index.html and the code in one obvious place.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
createRoot fails with Target container is not a DOM element
Uncaught Error: Target container is not a DOM element
createRoot needs a real DOM node.
3 causes, each with a fixWeb apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fix