简体   繁体   English

为什么使用“ npm install -g themodule”安装的模块不起作用?

[英]Why a module installed with “npm install -g themodule” doesn't work?

I'm working on a Windows environment. 我正在Windows环境下工作。

I install a module with 我安装一个模块

npm install -g themodule

The module is installed, I can see it with the command 该模块已安装,我可以使用以下命令查看它

npm list -g --depth 0

But when I do a require("themodule"), I get an the error: 但是当我执行一个require(“ themodule”)时,我得到一个错误:

Error: Cannot find module 'themodule'

I assume it's a rookie mistake but I don't see it. 我认为这是菜鸟的错误,但我看不到。

Did someone have an explanation? 有人有解释吗?

You are globally installing the package but not making it available in your node_modules folder for your project.. 您正在全局安装软件包,但未在项目的node_modules文件夹中使用它。

you need to give 你需要给

npm i --save-dev themodule // this installs a devDependency for development purpose npm i --save-dev themodule // //安装devDependency以进行开发

npm i --save themodule // this installs a dependency and is for production ready. npm i --save themodule //这将安装一个依赖项,并准备生产。

(please see.. npm i is equivalent for npm install ) (请参阅.. npm i等价于npm install

-- -

in short.. devDependencies object in package.json will have all the development related packages and dependency object will have production ready packages.. 总之.. devDependencies对象package.json将所有的开发相关的包和dependency对象有生产准备包..

So.. to summon this.. when you say require('themodule') , then node checks whether this dependency package is available in node_modules folder or not.. 所以..召唤这个..当你说require('themodule') ,node检查该依赖包在node_modules文件夹中是否可用。

so in your case it is not and that is the reason for the error.. 所以在您的情况下不是,这就是错误的原因。

Now, add the dependency package details in the package.json or do an npm i --save themodule 现在,在package.json添加依赖包的详细信息,或者执行npm i --save themodule

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

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