[英]Unable to resolve path to module 'react'. (import/no-unresolved)
Seems like i am missing something here, it should work without errors but eslint keeps throwing the following:好像我在这里遗漏了一些东西,它应该可以正常工作但eslint不断抛出以下内容:
Unable to resolve path to module 'react'.无法解析模块“反应”的路径。 (import/no-unresolved)
(导入/未解决)
Missing file extension for "react" (import/extensions)缺少“反应”的文件扩展名(导入/扩展)
when trying to import React from 'react'
尝试
import React from 'react'
时
here is some debug info:这是一些调试信息:
package.json package.json
{
"dependencies": {},
"devDependencies": {
"react": "16.3.2",
"react-dom": "16.3.2",
"@storybook/addon-actions": "^3.4.2",
"@storybook/addon-links": "^3.4.2",
"@storybook/addons": "^3.4.2",
"@storybook/react": "^3.4.2",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-runtime": "^6.26.0",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0"
}
}
.eslintrc .eslintrc
{
"parser": "babel-eslint",
"extends": ["airbnb", "prettier"],
"env": {
"browser": true,
"node": true,
"es6": true
}
}
.babelrc .babelrc
{
"presets": ["env", "react"]
}
editor : atom v1.26.1编辑:atom v1.26.1
Thanks.谢谢。
If you're using React Native it may help to add .native.js
as an allowed extension in your .eslintrc
file.如果你使用 React Native,在你的
.eslintrc
文件中添加.native.js
作为允许的扩展名可能会有所帮助。
Also, if you're using Typescript then .ts
and .tsx
will help too.此外,如果您使用的是 Typescript,那么
.ts
和.tsx
也会有所帮助。
"settings": {
"import/resolver": {
"node": {
"extensions": [".ts", ".tsx", ".native.js"]
}
}
}
I think it complains because react
should be in dependencies
:我认为它会抱怨,因为
react
应该dependencies
:
{
"dependencies": {
"react": "16.3.2",
"react-dom": "16.3.2",
},
"devDependencies": {
"@storybook/addon-actions": "^3.4.2",
"@storybook/addon-links": "^3.4.2",
"@storybook/addons": "^3.4.2",
"@storybook/react": "^3.4.2",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-runtime": "^6.26.0",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0"
}
}
I installed react and react-dom using npm i -E react react-dom
trying to install the exact version which didn't install it correctly.我使用
npm i -E react react-dom
安装了react和react-dom尝试安装未正确安装的确切版本。
npm i react react-dom -D
solved the problem. npm i react react-dom -D
解决了这个问题。
Just comment out the import and run.只需注释掉导入并运行即可。 Then again remove the comments.
然后再次删除评论。 It worked for me.
它对我有用。
I have experience with the same problem.我有同样问题的经验。
In my case, this error appear because I pull new update from the remote repository and it's bring new dependencies.就我而言,出现此错误是因为我从远程存储库中提取了新更新并带来了新的依赖项。
To solve this, I just install that dependencies with npm install
为了解决这个问题,我只需使用
npm install
安装该依赖项
我遇到了一些问题,我从项目中删除了 nodo_modules 目录并运行 yarn install / npm install
This also happened to me.这也发生在我身上。 In my case, it was because I was running
npm
version 6, but a team member had installed a new library via npm
version 7. Version 7 uses a new version for the lock file format.就我而言,这是因为我运行的是
npm
版本 6,但是团队成员通过npm
版本 7 安装了一个新库。版本 7 使用新版本的锁文件格式。
Our solution was to make sure everyone was running the same npm
version so that our package-lock.json
files would be consistent.我们的解决方案是确保每个人都运行相同的
npm
版本,以便我们的package-lock.json
文件保持一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.