简体   繁体   English

npm测试时“找不到模块”如何解决

[英]How can I solve "cannot find module" when npm test

I am using create-react-app.我正在使用 create-react-app。 When I input 'npm test', an error occurring "Cannot find module".当我输入“npm test”时,出现错误“找不到模块”。 I though jest is already installed in create-react-app.我虽然开玩笑已经安装在 create-react-app 中。 The following is my package.json.下面是我的package.json。 Any ideas?有任何想法吗?

{
  "proxy": "http://localhost:5000",
  "name": "276_project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "axios": "^0.19.2",
    "bulma": "^0.9.0",
    "jwt-decode": "^2.2.0",
    "node-sass": "^4.14.1",
    "react": "^16.13.1",
    "react-bootstrap": "^1.2.2",
    "react-cart-components": "^2.1.0",
    "react-dom": "^16.13.1",
    "react-hook-form": "^5.7.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.1",
    "react-stripe-checkout": "^2.6.3",
    "react-toastify": "^6.0.6",
    "serve": "^11.3.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "heroku-postbuild": "npm run build"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I m working with my own starter pack but i guess problem is the same.Simply jest cant see our included modules.我正在使用自己的入门包,但我想问题是一样的。只是开玩笑看不到我们包含的模块。 First install yarn add identity-obj-proxy -D or npm install identity-obj-proxy -D ."-D" flag here is for my OS Ubuntu 20.04,for other OS-es should use "--save-dev" flag or something like that.首先安装yarn add identity-obj-proxy -Dnpm install identity-obj-proxy -D 。“-D”标志用于我的操作系统 Ubuntu 20.04,其他操作系统应使用“--save-dev”标志或类似的东西。 Then,you should find where in "create-react-app" is your "jest.config.js" and in part "moduleNameMapper" set as in mine config bellow.然后,您应该在“create-react-app”中找到您的“jest.config.js”和部分“moduleNameMapper”设置,如下面的我的配置。 Works well with typescript && javascript.适用于 typescript && javascript。

 module.exports = { preset: 'ts-jest', roots: ['<rootDir>/src'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], transform: { '\\.(ts|tsx)$': 'ts-jest', }, setupFiles: ['raf/polyfill'], testRegex: '/__tests__/.*\\.(ts|tsx|js)$', moduleNameMapper: { '^.+\\.(s?css|less|jpg|png|svg)$': 'identity-obj-proxy', }, setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'], snapshotSerializers: [], };

SOLUTION 1:解决方案 1:

you can use package-lock.json file, which is automatically generated when npm modifies your node_modules directory.您可以使用 package-lock.json 文件,该文件在 npm 修改您的 node_modules 目录时自动生成。 Therefore you can leave out checking in packages, because the package-lock.json tracks the exact versions of your node_modules, you're currently using.因此,您可以省略签入包,因为 package-lock.json 跟踪您当前正在使用的 node_modules 的确切版本。 To install packages from package-lock.json instead of package.json use the command npm ci.要从 package-lock.json 而不是 package.json 安装软件包,请使用命令npm ci.

SOLUTION 2:解决方案 2:

Delete your node_modules folder, & run npm install again删除你的 node_modules 文件夹,并run npm install again

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM