[英]How do I use newly installed modules in React when they do not appear in the package.json "dependencies" list?
[Updated my question to give more details] [更新了我的问题以提供更多详细信息]
I am quite new at working with React so this maybe a basic question.我对使用 React 很陌生,所以这可能是一个基本问题。 I installed several modules I will use one as an example for clarity which was
@react-google-maps/api
我安装了几个模块,为了清楚起见,我将使用一个作为示例,它是
@react-google-maps/api
In my PCs terminal I wrote the following for the install:在我的 PC 终端中,我为安装编写了以下内容:
npm i -S @react-google-maps/api
When I check in the terminal using:当我使用以下方式检查终端时:
npm ls --depth=0
I see the module is present in the list it returns.我看到该模块存在于它返回的列表中。 (I have Node.js version 16.14.2 and npm version 8.11.0)
(我有 Node.js 版本 16.14.2 和 npm 版本 8.11.0)
When I open my React app (created using the create-react-app template) I do not see the module listed under dependencies or in the node-modules list.当我打开我的 React 应用程序(使用 create-react-app 模板创建)时,我看不到依赖项下或节点模块列表中列出的模块。
Since I am some trouble using the contents of this module I was hoping someone might advise if there is something I did wrong in the install or need to do before trying to import components from it.由于我在使用这个模块的内容时遇到了一些麻烦,我希望有人可以建议我是否在安装中做错了什么,或者在尝试从中导入组件之前需要做些什么。
Honestly this happens all the time to me -- not knowing much about your local environment, I would wager that you're likely not in your project's directory and are probably installing it globally.老实说,这一直发生在我身上——对你的本地环境了解不多,我敢打赌你可能不在你的项目目录中,并且可能正在全局安装它。
If you're using VS code, you can just open the terminal in it, but if you're not go ahead and move to the directory that is at the same root level as your package.json.如果您使用的是 VS 代码,您可以在其中打开终端,但如果您不继续并移动到与您的 package.json 处于同一根级别的目录。 Then do your npm install [DEPENDENCY YOU WANT]
然后做你的 npm install [DEPENDENCY YOU WANT]
It should work!它应该工作!
如果您使用的是版本 5 之前的 npm 版本,则必须在安装模块时添加--save
(或-S
)选项以将它们添加到package.json
(例如npm install --save lodash
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.