react Cannot find module

song@u:~/src/JavaProject/demo-app/goods-front$ yarn start
yarn start v0.27.5
$ react-scripts start
module.js:471
    throw err;
    ^


Error: Cannot find module '../scripts/start'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.resolve (internal/module.js:27:19)
    at Object.<anonymous> (/home/song/src/JavaProject/demo-app/goods-front/node_modules/.bin/react-scripts:30:25)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)

error Command failed with exit code 1.


先说明我是Linux环境

导致该问的原因是:node_modules/.bin 目录下本来应该都是链接文件,但不知为何,在创建工程时都变成了实体文件,导致js中的相对路径取值错误,所以就报找不到 module的错误。

正确情况下使用(进入node_modules/.bin目录) 使用 ls -al 命令查看可以发现都是 ln 文件。

song@u:~/src/JavaProject/demo-app/goods-front1/node_modules/.bin$ ls -al
total 40
drwxrwxr-x   2 song song  4096 9月   8 15:55 .
drwxrwxr-x 891 song song 36864 9月   8 15:56 ..
lrwxrwxrwx   1 song song    18 9月   8 15:55 acorn -> ../acorn/bin/acorn
lrwxrwxrwx   1 song song    26 9月   8 15:55 ansi-html -> ../ansi-html/bin/ansi-html
lrwxrwxrwx   1 song song    25 9月   8 15:55 babylon -> ../babylon/bin/babylon.js
lrwxrwxrwx   1 song song    48 9月   8 15:55 browserslist -> ../autoprefixer/node_modules/browserslist/cli.js
lrwxrwxrwx   1 song song    20 9月   8 15:55 cssesc -> ../cssesc/bin/cssesc
lrwxrwxrwx   1 song song    16 9月   8 15:55 csso -> ../csso/bin/csso
lrwxrwxrwx   1 song song    34 9月   8 15:55 detect -> ../detect-port-alt/bin/detect-port
lrwxrwxrwx   1 song song    34 9月   8 15:55 detect-port -> ../detect-port-alt/bin/detect-port
lrwxrwxrwx   1 song song    15 9月   8 15:55 errno -> ../errno/cli.js
lrwxrwxrwx   1 song song    29 9月   8 15:55 escodegen -> ../escodegen/bin/escodegen.js
lrwxrwxrwx   1 song song    30 9月   8 15:55 esgenerate -> ../escodegen/bin/esgenerate.js
lrwxrwxrwx   1 song song    23 9月   8 15:55 eslint -> ../eslint/bin/eslint.js
lrwxrwxrwx   1 song song    25 9月   8 15:55 esparse -> ../esprima/bin/esparse.js
lrwxrwxrwx   1 song song    28 9月   8 15:55 esvalidate -> ../esprima/bin/esvalidate.js
lrwxrwxrwx   1 song song    28 9月   8 15:55 handlebars -> ../handlebars/bin/handlebars
lrwxrwxrwx   1 song song    12 9月   8 15:55 he -> ../he/bin/he
lrwxrwxrwx   1 song song    23 9月   8 15:55 html-minifier -> ../html-minifier/cli.js
lrwxrwxrwx   1 song song    21 9月   8 15:55 internal-ip -> ../internal-ip/cli.js
lrwxrwxrwx   1 song song    23 9月   8 15:55 jest -> ../jest-cli/bin/jest.js
lrwxrwxrwx   1 song song    35 9月   8 15:55 jest-runtime -> ../jest-runtime/bin/jest-runtime.js
lrwxrwxrwx   1 song song    18 9月   8 15:55 jsesc -> ../jsesc/bin/jsesc
lrwxrwxrwx   1 song song    19 9月   8 15:55 json5 -> ../json5/lib/cli.js
lrwxrwxrwx   1 song song    25 9月   8 15:55 js-yaml -> ../js-yaml/bin/js-yaml.js
lrwxrwxrwx   1 song song    22 9月   8 15:55 loose-envify -> ../loose-envify/cli.js
lrwxrwxrwx   1 song song    32 9月   8 15:55 miller-rabin -> ../miller-rabin/bin/miller-rabin
lrwxrwxrwx   1 song song    14 9月   8 15:55 mime -> ../mime/cli.js
lrwxrwxrwx   1 song song    20 9月   8 15:55 mkdirp -> ../mkdirp/bin/cmd.js
lrwxrwxrwx   1 song song    23 9月   8 15:55 multicast-dns -> ../multicast-dns/cli.js
lrwxrwxrwx   1 song song    14 9月   8 15:55 rc -> ../rc/index.js
lrwxrwxrwx   1 song song    37 9月   8 15:55 react-scripts -> ../react-scripts/bin/react-scripts.js
lrwxrwxrwx   1 song song    25 9月   8 15:55 regjsparser -> ../regjsparser/bin/parser
lrwxrwxrwx   1 song song    16 9月   8 15:55 rimraf -> ../rimraf/bin.js
lrwxrwxrwx   1 song song    18 9月   8 15:55 sane -> ../sane/src/cli.js
lrwxrwxrwx   1 song song    20 9月   8 15:55 semver -> ../semver/bin/semver
lrwxrwxrwx   1 song song    16 9月   8 15:55 sha.js -> ../sha.js/bin.js
lrwxrwxrwx   1 song song    23 9月   8 15:55 sshpk-conv -> ../sshpk/bin/sshpk-conv
lrwxrwxrwx   1 song song    23 9月   8 15:55 sshpk-sign -> ../sshpk/bin/sshpk-sign
lrwxrwxrwx   1 song song    25 9月   8 15:55 sshpk-verify -> ../sshpk/bin/sshpk-verify
lrwxrwxrwx   1 song song    22 9月   8 15:55 strip-indent -> ../strip-indent/cli.js
lrwxrwxrwx   1 song song    16 9月   8 15:55 svgo -> ../svgo/bin/svgo
lrwxrwxrwx   1 song song    21 9月   8 15:55 sw-precache -> ../sw-precache/cli.js
lrwxrwxrwx   1 song song    52 9月   8 15:55 uglifyjs -> ../html-minifier/node_modules/uglify-js/bin/uglifyjs
lrwxrwxrwx   1 song song    37 9月   8 15:55 uuid -> ../request/node_modules/uuid/bin/uuid
lrwxrwxrwx   1 song song    25 9月   8 15:55 webpack -> ../webpack/bin/webpack.js
lrwxrwxrwx   1 song song    47 9月   8 15:55 webpack-dev-server -> ../webpack-dev-server/bin/webpack-dev-server.js
lrwxrwxrwx   1 song song    18 9月   8 15:55 which -> ../which/bin/which

在使用Jest进行测试时,出现“Cannot find module from”错误通常是由于模块导入路径不正确或模块未正确导出。以下是一些解决方法: 1. **检查模块导入路径**: 确保你在测试文件中导入模块的路径是正确的。路径错误是最常见的原因之一。 ```javascript // 错误示例 import myModule from '../src/my-module'; // 正确示例 import myModule from '../src/myModule'; ``` 2. **确保模块已导出**: 确保你要导入的模块已经正确导出。你可以使用`export`或`module.exports`来导出模块。 ```javascript // 使用 ES6 导出 // myModule.js export const myFunction = () => { // ... }; // 使用 CommonJS 导出 // myModule.js const myFunction = () => { // ... }; module.exports = { myFunction }; ``` 3. **配置Babel或TypeScript**: 如果你使用Babel或TypeScript进行转译,确保你的配置文件(如`.babelrc`或`tsconfig.json`)正确配置,并且相关的依赖已安装。 ```json // .babelrc { "presets": ["@babel/preset-env", "@babel/preset-react"] } // tsconfig.json { "compilerOptions": { "module": "commonjs", "target": "es6", "jsx": "react", "strict": true, "esModuleInterop": true } } ``` 4. **安装依赖**: 确保所有依赖已正确安装。运行以下命令来安装依赖: ```bash npm install ``` 5. **使用`moduleNameMapper`配置**: 在`jest.config.js`中配置`moduleNameMapper`,以处理模块别名或特定路径。 ```javascript // jest.config.js module.exports = { moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1', }, }; ``` 通过以上步骤,你应该能够解决“Cannot find module from”错误。如果问题依然存在,请检查控制台输出,确保没有其他相关错误信息,并确保模块路径和导出方式正确。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

songtao542

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值