简体   繁体   English

当新安装的模块没有出现在 package.json “依赖项”列表中时,如何在 React 中使用它们?

[英]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.

相关问题 我执行npm安装时未显示package.json grunt依赖模块 - package.json grunt dependences modules don't show up when i do a npm install 如何将所有已安装的节点模块保存在package.json中? - How can I save all the installed node modules in package.json? 如何自动更新 package.json 中的条目? - How do I update the entries in package.json automatically? 如何在我的反应项目中使用 package.json 依赖项中的系统变量? - How to use system variables in package.json dependencies in my react project? package.json中如何使用override更新子依赖 - How to use override in package.json to update child dependencies 当我在package.json中更改了一个必需的模块时,如何重新加载webpack? - How can I reload the webpack when I changed one of my required modules in package.json? React-chart.js 2 模块错误但已安装在 package.json 文件中 - React-chart.js 2 Error in modules but was already installed in the package.json file 在 react package.json 文件中,我是否将 @capacitor/android 安装为开发依赖项? - In react package.json file, do I install @capacitor/android as dev dependency? 我如何使用GruntFile.coffee和package.json来提取/ make / fetch / ?? Lungo.js示例文件? - How do I use GruntFile.coffee and package.json to extract/make/fetch/?? the Lungo.js example files? 如何在没有 package.json 文件的情况下运行 JavaScript 项目,从而在运行时出错? - How do I run a JavaScript project with no package.json file present, which therefore gives error when running it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM