Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught Error: Target container is not a DOM element. #2615

Closed
pelotom opened this issue Jan 1, 2018 · 31 comments
Closed

Uncaught Error: Target container is not a DOM element. #2615

pelotom opened this issue Jan 1, 2018 · 31 comments

Comments

@pelotom
Copy link
Contributor

pelotom commented Jan 1, 2018

I am getting this error since upgrading from 3.2.18 to 3.3.3:

Uncaught Error: Target container is not a DOM element.
    at invariant (invariant.js:42)
    at renderSubtreeIntoContainer (react-dom.development.js:15180)
    at Object.render (react-dom.development.js:15290)
    at exports.default (routes.js:39)
    at App.init (app.js:119)
    at exports.default (index.js:41)
    at Object.<anonymous> (index.js:16)
    at __webpack_require__ (bootstrap 77c5cb83d0653d7a60e4:678)
    at fn (bootstrap 77c5cb83d0653d7a60e4:88)
    at Object.defineProperty.value (fuse.js:996)

And the page is just showing the following text:

/static/media/index.html.3cbc7277.ejs
@ndelangen
Copy link
Member

@pelotom Thanks for opening up an issue, I've seen this error during development on 3.3.
It was when running storyshots, We fixed it there, but it seems you're experiencing it in a different scenario.

Can you make sure all storybook related packages are at the same version?

It looks like it's related to a webpack config. We use the HTMLplugin with a template, seems that's where the problem might be.

@ndelangen
Copy link
Member

Are you using a custom webpack config? If so, can you share it?

@aaronfullerton
Copy link
Contributor

@ndelangen I'm also getting this exact error using a custom webpack config since upgrading to 3.3.

If it's helpful, here's my webpack config, which is more or less the version that ships with create-react-app.

@pelotom
Copy link
Contributor Author

pelotom commented Jan 2, 2018

I’m also using a webpack config derived from CRA.

@ndelangen
Copy link
Member

Do you have a repo I can checkout @aaronfullerton or @pelotom ?

I'm trying the webpack config @aaronfullerton provided, but it's demanding a lot of time getting it setup in a way for me to reproduce.

@aaronfullerton
Copy link
Contributor

@ndelangen sure, You can try this: https://github.com/aaronfullerton/storybook-issue-2615

I pulled this from our existing codebase and attempted to remove as much noise as possible. My apologies if there's still a bit of unnecessary code in there.

@ndelangen
Copy link
Member

Thank you, I have found the solution to the problem thanks to your reproduction repo ❤️

Here's the required change:
aaronfullerton/storybook-issue-2615#1

@ndelangen
Copy link
Member

Please let me know if this doesn't solve the problem for you, or something else breaks.

I'll close the issue, but happy to re-open if it still goes 💥

@aaronfullerton
Copy link
Contributor

Excellent, thank you. I can confirm this solves the problem. I'll look into updating the docs to indicate this.

@loadingwyn
Copy link

loadingwyn commented Jan 16, 2018

@ndelangen
I find this error in3.3.9 too. I’m also using a webpack config derived from CRA. If I remove file-loader from the config, this problem can be solved.
Here is the file-loader config:

{
                exclude: [
                    /\.html$/,
                    /\.(js|jsx)$/,
                    /\.css$/,
                    /\.scss$/,
                    /\.json$/,
                    /\.bmp$/,
                    /\.gif$/,
                    /\.jpe?g$/,
                    /\.png$/,
                ],
                loader: 'file-loader',
                options: {
                    name: 'static/media/[name].[hash:8].[ext]',
                },
            },

@pelotom
Copy link
Contributor Author

pelotom commented Jan 16, 2018

You don't have to remove the file loader, just exclude *.ejs:

{
  exclude: [
    /\.html$/,
    /\.(js|jsx)$/,
    /\.css$/,
    /\.scss$/,
    /\.json$/,
    /\.bmp$/,
    /\.gif$/,
    /\.jpe?g$/,
    /\.png$/,
+   /\.ejs$/,
  ],
  loader: 'file-loader',
  options: {
    name: 'static/media/[name].[hash:8].[ext]',
  },
},

@loadingwyn
Copy link

It works. Thank you!

@switz
Copy link

switz commented Mar 7, 2018

What if my webpack config doesn't use file-loader and I'm seeing this?

Running:

    "@storybook/addon-actions": "^3.3.15",
    "@storybook/addon-info": "^3.3.15",
    "@storybook/addon-links": "^3.3.15",
    "@storybook/addon-options": "^3.3.15",
    "@storybook/addons": "^3.3.15",
    "@storybook/react": "^3.3.15",

Webpack config:

const path = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.yaml$/,
        loaders: ['json-loader', 'yaml-loader'],
        include: path.resolve(__dirname, '../../i18n')
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(eot|woff|woff2|ttf|svg)$/,
        use: {
          loader: 'url-loader',
          query: {
            limit: 30000,
            name: '[name].[ext]',
          },
        },
      },
    ],
  },
};

storybook .babelrc

{
  "presets": ["react-app"],
  "plugins": [
    ["emotion", { "autoLabel": true }],
    "jsonify-css"
  ]
}

@Hypnosphi
Copy link
Member

And the page is just showing the following text:

/static/media/index.html.3cbc7277.ejs

Is it the case for you @switz ?

@sathiyaanbu
Copy link

I got the same error : Target container is not a DOM element.

Here is my webpack.config,.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
};

"babel-core": "^6.26.3",
   "babel-loader": "^7.1.5",
   "babel-preset-env": "^1.7.0",
   "babel-preset-react": "^6.24.1",
   "css-loader": "^1.0.0",
   "html-webpack-plugin": "^3.2.0",
   "json-loader": "^0.5.7",
   "style-loader": "^0.21.0",
   "webpack": "^4.15.1",
   "webpack-cli": "^3.0.8",
   "webpack-dev-server": "^3.1.4"


"axios": "^0.18.0",
 "prop-types": "^15.6.2",
 "react": "^16.4.1",
 "react-dom": "^16.4.1",
 "react-redux": "^5.0.7",
 "reactstrap": "^6.3.0",
 "redux": "^4.0.0",
 "redux-promise": "^0.6.0",
 "redux-thunk": "^2.3.0"

Any solutions pls?

@ndelangen
Copy link
Member

@sathiyaanbu The webpack config you provide is getting merged with a base-config..
The base already has everything you add to it. This causes the html-plugin to be registered twice. That results in the error you see, I think.

Try removing your webpack config completely or removing the html-webpack plugin from your storybook's webpack config.

@ndelangen
Copy link
Member

@sathiyaanbu that doesn't seem like a storybook issue, sorry.

I highly recommend you fill your component-stories with mockdata, not with data fetched from an API.

@ghost
Copy link

ghost commented Jul 15, 2018

If there is someone who hasn't fix the problem jet, check the order of your tag elements in html.
For example I wrote ( accidentally )

    <script src="/app/something.js"></script>
    <div id="root"> x </div>

But the script tag should be below the DOM element.
I've lost 2h on this :/
Cheers!

@ndelangen
Copy link
Member

Ow damn that's not fun @mandy94.

What could we have done that could have prevented that from happening?
Is it really storybook related?

@epoz
Copy link

epoz commented Sep 3, 2018

Yowzers, thanks for the heads-up @mandy94 that saved me a lot of time.

@chiholiu10
Copy link

Check in your html file if the id attribute has the same name as
ReactDOM.render(, document.getElementById('index'));

tgandrews added a commit to ONSdigital/eq-author that referenced this issue Oct 8, 2018
As part of an unintentional bump of the storybook version in
e8f9c1d it would no longer run
correctly.

The issue was already reported and relates to our webpack config
derived from CRA. storybookjs/storybook#2615
tgandrews added a commit to ONSdigital/eq-author that referenced this issue Oct 9, 2018
As part of an unintentional bump of the storybook version in
e8f9c1d it would no longer run
correctly.

The issue was already reported and relates to our webpack config
derived from CRA. storybookjs/storybook#2615
tgandrews added a commit to ONSdigital/eq-author that referenced this issue Oct 10, 2018
As part of an unintentional bump of the storybook version in
e8f9c1d it would no longer run
correctly.

The issue was already reported and relates to our webpack config
derived from CRA. storybookjs/storybook#2615
@dgwyer
Copy link

dgwyer commented Nov 13, 2018

@mandy94 Thanks too! Was just a simple thing like this that caused these errors. Fixed now. :)

@BinitaBharati
Copy link

I bumped into this thread while searching for "React Target container is not a DOM element". This is not related to storybook, nevertheless, I will leave my comments here, as it may help some other person. So, I was facing the same error with minimal React.js code. I had a basic html with a empty target div, which had to be populated by React js code. As mentioned by @mandy94, moving the dom container before the script tag worked.

@casprwang
Copy link

casprwang commented Mar 17, 2019

It looks like an error indicating the "root" dom element for targeting/mounting from js was not found.

@bensampaio
Copy link

I am having this exact same problem apparently because I am changing the id of root to app-root. I am doing this because I would like the dom in my storybook to look just like the dom of my application. This also makes it easier to test modals and so on. Currently I am doing this via a global decorator that basically does this:

const appRoot = document.getElementById('root');

appRoot.setAttribute('id', 'app-root');

Is there any better way of doing this?

@ndelangen
Copy link
Member

How about wrapping it?

<div id="app-root">
  <div id="root">
  </div>
</div>

?

@Jinujs
Copy link

Jinujs commented Jul 14, 2020

please add these lines to your index.js file

ReactDOM.render(
<React.StrictMode>
// render your component
</React.StrictMode>,
document.getElementById("root")
);

@sideeq12
Copy link

sideeq12 commented Nov 8, 2020

I'm having same issue "Target container is not a DOM"
and I specify <script></script> below the

@pmongkho
Copy link

pmongkho commented Feb 9, 2021

image

is this the same issue? i have the right lines of code but it doesnt resolve, is it a webpack error?

@newcanopies
Copy link

hi @ndelangen

there is no root div in my index.html

image

@sunidhi56
Copy link

image

is this the same issue? i have the right lines of code but it doesnt resolve, is it a webpack error?

how did you solve this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests