node.js npm 不会在本地安装软件包。怎么了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10985097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
npm won't install packages locally. What's wrong?
提问by user1449536
I want to install packages locally, but npm is always installing packages to the global location. I'm running the following command:
我想在本地安装包,但 npm 总是将包安装到全局位置。我正在运行以下命令:
npm install serialport
I do not have a .npmrc command and I'm not using the -g flag, so I don't know why it's not installing locally. Here's a snippet from the config dump showing that global is false: $ npm config ls -l | grep global global = false globalconfig = "/usr/local/etc/npmrc" globalignorefile = "/usr/local/etc/npmignore"
我没有 .npmrc 命令并且我没有使用 -g 标志,所以我不知道为什么它没有在本地安装。这是配置转储中的一个片段,显示 global 为 false: $ npm config ls -l | grep global global = false globalconfig = "/usr/local/etc/npmrc" globalignorefile = "/usr/local/etc/npmignore"
And the packages are still being installed like this
并且软件包仍然像这样安装
[email protected] ../../../../node_modules/serialport
So unless I am totally wrong about what "local" means, this seems wrong. I was under the impression that "local" meant in the current working directory so that I could do a "require" in my main code file. See: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/as referenced in a previous npm related question.
因此,除非我对“本地”的含义完全错误,否则这似乎是错误的。我的印象是“本地”意味着在当前工作目录中,这样我就可以在主代码文件中执行“要求”。请参阅:http: //blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/,如之前的 npm 相关问题所述。
Can someone please give me some hints on this? Thank you very much.
有人可以给我一些提示吗?非常感谢。
P.S. It's not specific to the serialport module. It's happening with all of them.
PS 这不是特定于串行端口模块。他们所有人都在发生这种情况。
回答by Pickels
Most of my answer can be found: http://npmjs.org/doc/folders.html#More-Information
我的大部分答案都可以找到:http: //npmjs.org/doc/folders.html#More-Information
What I understand is that npm will try to install it in a sensible location. So if you have a project/node_modulesdirectory and you are in /project and do npm install it will use product/node_modules.
我的理解是 npm 会尝试将它安装在一个合理的位置。因此,如果您有一个project/node_modules目录并且您在 /project 中并执行 npm install 它将使用product/node_modules.
Now if you accidentally did a cd project/cssand do npm install then npm will traverse up until it finds your node_modulesdirectory. This is to prevent you from accidentally installing it in your project/css.
现在,如果您不小心执行了 cdproject/css并执行了 npm install,那么 npm 将向上遍历,直到找到您的node_modules目录。这是为了防止您不小心将它安装在您的project/css.
So in your case you have a node_module directory somewhere in the path of your project. So my guess is that you can fix it by either deleting that directory or manually creating a node_modulesdir in your project folder.
因此,在您的情况下,您的项目路径中有一个 node_module 目录。所以我的猜测是您可以通过删除该目录或node_modules在您的项目文件夹中手动创建一个目录来修复它。
回答by Cirilo Meggiolaro
If you have a package.json file on the folder you are trying to install the package, then it will create the node_modules folder correctly.
如果您尝试安装包的文件夹中有 package.json 文件,那么它将正确创建 node_modules 文件夹。
Basic package.json
基本包.json
{
"name": "application-name",
"version": "0.0.1"
}

