Enzyme: cannot find 'react/addons'

Created on 21 Dec 2016  ·  14Comments  ·  Source: enzymejs/enzyme

when I try to run my tests using karma i get the following error from enzyme:

[Error: Cannot find module 'react/addons' from '[path to project]node_modules\enzyme\build']

Any ideas why?

These are my deps:

    "react": "^15.3.2",
    "react-addons-css-transition-group": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^5.0.0",
    "react-router": "^3.0.0",
    "react-widgets": "^3.4.5",

and my dev deps:

    "chai-enzyme": "^0.6.1",
    "cheerio": "^0.22.0",
    "enzyme": "^2.6.0",
    "karma": "^1.3.0",
    "karma-chai": "^0.1.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-coverage": "^1.1.1",
    "karma-ie-launcher": "^1.0.0",
    "karma-mocha": "^1.3.0",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-sinon": "^1.0.5",
    "karma-typescript": "^2.1.5",
    "mocha": "^3.2.0",
    "phantomjs": "^2.1.7",
    "react-addons-test-utils": "^15.4.1",

All deps are up to date.

Most helpful comment

I was able to have it running. I was adding externals to karma instead of webpack, such a stupid mistake!

@ljharb I actually need cheerio in the list of externals, without it the tests are not running and throw the following error:

ERROR in ./~/cheerio/package.json
Module parse failed: /Users/Me/project/react-rock-paper-scissors/node_modules/cheerio/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.

All 14 comments

@sir-marc react-addons-test-utils needs to have an identical version to react and react-dom - all of them must go in lockstep.

@ljharb Yes they are all the same version. Now I have updated the package.json so every react module has the version 15.4.1
But i still get the error.

When I was using karma with webpack i had to add 'react/addons' to the webpack externals. As I switched to karma-typescript i dont need webpack anymore.

@sir-marc have you seen the webpack setup docs?

@blainekasten the problem was, that im no longer using webpack. I use karma-typescript instead. I solved it that way: https://github.com/monounity/karma-typescript/issues/54

Still doesn't work for me. I'm using React 15.4.2 and same version of react-dom and react-addons- libraries. When I npm ls, there are no other versions of the above libraries in the tree. I'm running tests with Mocha Webpack.

@steve-taylor can you share your npm ls output?

@steve-taylor I am experiencing a similar issue, did you manage to solve it?

Same issue too.
I run the same version of react, react-dom and react-addons-test-utils (15.4.2).
I followed the documentation to set up the external dependencies.

externals: {
    'cheerio': 'window',
    'react/addons': true,
    'react/lib/ExecutionEnvironment': true,
    'react/lib/ReactContext': true
},

I get the following error when I run the tests:

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Cannot resolve module 'react/addons' in /Users/Me/project/node_modules/enzyme/build
 @ ./~/enzyme/build/react-compat.js 41:16-39 42:46-69

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Cannot resolve module 'react/lib/ReactContext' in /Users/Me/project/node_modules/enzyme/build
 @ ./~/enzyme/build/react-compat.js 43:23-56

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Cannot resolve module 'react/lib/ExecutionEnvironment' in /Users/Me/project/node_modules/enzyme/build
 @ ./~/enzyme/build/react-compat.js 73:4-45

@alexmngn I'm not sure if you need cheerio with react 15 and webpack 2

I was able to have it running. I was adding externals to karma instead of webpack, such a stupid mistake!

@ljharb I actually need cheerio in the list of externals, without it the tests are not running and throw the following error:

ERROR in ./~/cheerio/package.json
Module parse failed: /Users/Me/project/react-rock-paper-scissors/node_modules/cheerio/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.

@alexmngn I was doing the same thing 🤦‍♀️ , took me hours of debugging, thank youuuu! :D

Hey guys, I am having the same issue: Error: Cannot find module 'react/addons'
this is my package local:

  "dependencies": {
    "browser-sync": "^2.23.6",
    "browserify": "^16.1.1",
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "gulp": "^3.9.1",
    "gulp-live-server": "0.0.31",
    "react": "^16.2.0",
    "reactify": "^1.1.1",
    "vinyl-source-stream": "^2.0.0"
  }

and this is my global:

PS C:\xampp\htdocs\express> npm ls -g --depth 0
C:\Users\USER\AppData\Roaming\npm
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]

@ereztdev can you file a new issue?

@ljharb sure.

Was this page helpful?
0 / 5 - 0 ratings