Jest - Cannot find module 'source-map' from 'source-map-support.js'

Viewed 16717

I've got the problem that I get the error:

Cannot find module 'source-map' from 'source-map-support.js'
Ran all test suites matching /Foo-test/i.
T:\public_html\testProj>jest Foo-test
FAIL  //XXXX.de/XXXX/Users/XXX/public_html/testProj/src/__tests__/Foo-test.js
? Test suite failed to runtml/testProj/src/__tests__/Foo-test.js
Cannot find module 'source-map' from 'source-map-support.js'
at Resolver.resolveModule (T:/public_html/testProj/node_modules/jest-resolve/build/index.js:221:17)

If I try to run a test with Jest.

My .babelrc looks like this:

{
  "presets": ["env", "react"],
  "sourceMaps": "both",
  "plugins": [
    "add-module-exports",
    "react-hot-loader/babel",
    "source-map-support"
  ]
}

My package.json looks like this:

{
  "name": "testproject",
  "version": "1.8.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "devDependencies": {
    "@babel/runtime": "^7.0.0-rc.1",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.3",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "copy-webpack-plugin": "^4.5.2",
    "css-hot-loader": "^1.3.9",
    "eslint": "4.9.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-import": "2.7.0",
    "eslint-plugin-jsx-a11y": "6.0.2",
    "eslint-plugin-react": "7.4.0",
    "file-loader": "^1.1.11",
    "foundation-sites": "^6.4.4-rc1",
    "node-sass": "^4.8.3",
    "prettier": "^1.14.2",
    "react-hot-loader": "4.1.3",
    "sass-loader": "^6.0.7",
    "source-map": "^0.7.3",
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.9",
    "webpack-dev-server": "^3.0.0"
  },
  "dependencies": {
    "@material-ui/core": "^1.5.0",
    "axios": "^0.18.0",
    "babel-jest": "^23.6.0",
    "chai": "^4.2.0",
    "core-js": "^2.5.3",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0",
    "jest": "^23.6.0",
    "prop-types": "^15.6.0",
    "react": "^16.3.0",
    "react-accessible-accordion": "^2.4.4",
    "react-dom": "^16.3.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "react-string-replace": "^0.4.1",
    "redux": "^3.7.2",
    "redux-thunk": "^2.3.0",
    "sinon": "^7.0.0"
  },
  "scripts": {
    "dev": "webpack-dev-server --hot",
    "build": "webpack --colors --profile --progress --env.mode production",
    "lint": "eslint ./src/ --ext .js,.jsx",
    "test": "jest"
  },
  "jest": {
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx"
    ]
  }
}

The test itself seems to be ok, but I get the error nevertheless.

I tried different solutions:

-Some people say it would be necessary to add these configs to the package.json:

"jest": {
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx"
    ]
  }

But I had no luck with that either. Can someone help here?

5 Answers

Hit this issue when using a Jest test runner in VS Code. Taking a pointer from https://github.com/facebook/jest/issues/6880#issuecomment-441499110, I noticed that my jest.config.js contained

    moduleDirectories: ["<rootDir>/node_modules", "<rootDir>/ClientApp"]

Changing it to

    moduleDirectories: ["node_modules", "ClientApp"]

fixed the problem.

I resolve the problem by running the project and node in the same drive.

i.e. previously my code is put in Q:/ drive, and my nodejs is install in C:/ drive. and it keeps saying Cannot find module 'source-map' from 'source-map-support.js' when I run jest.

once after I open my project in C:/, then it works.

Make sure to have:

moduleDirectories: ["node_modules"]

not:

moduleDirectories: ["/node_modules/"]

and don't omit the node_modules here.

I discovered a new situation,another possibility is an error during installation, reinstall ts-node to fix the problem

FYI this just happened to me at work. I'm not enough of an expert to understand what's going on under the hood, but the root of my problem was that I had my project running inside webpack in dev mode in a primary terminal, at the same time I was trying to run the tests in a 2nd terminal.

Once I stopped the dev server, the tests ran just fine afterwards.

Related