简体   繁体   English

将 NPM 依赖安装到不包含 package.json 的文件夹中

[英]Install NPM dependency into folder that does not contain package.json

I have a directory that looks like so我有一个看起来像这样的目录

/foo

yep it's an empty folder, I want to run是的,它是一个空文件夹,我想运行

cd foo && npm install bar

however npm is complaining that there is no package.json file in the foo directory.但是 npm 抱怨 foo 目录中没有 package.json 文件。

Is there a bonafide reliable way to install a depedency into a directory if there is no package.json file there (yet)?如果那里(还)没有 package.json 文件,是否有一种真正可靠的方法将依赖项安装到目录中?

Turns out, it was just a warning, not a error, I misread, it says:原来,这只是一个警告,而不是一个错误,我误读了,它说:

npm WARN enoent ENOENT: no such file or directory, open '/home/olegzandr/.suman/package.json' npm WARN enoent ENOENT: 没有这样的文件或目录,打开'/home/olegzandr/.suman/package.json'

I guess my question then becomes, is there a way to tell NPM to ignore a missing package.json file?我想我的问题变成了,有没有办法告诉 NPM 忽略丢失的 package.json 文件?

删除 package-lock.json 为我解决了。

One of the primary reasons for a package.json is to document the dependencies that a given application has. package.json 的主要原因之一是记录给定应用程序具有的依赖关系。 With that said, you can either run npm init in your directory to create a package.json or install the package globally using npm install -g bar .话虽如此,您可以在目录中运行npm init以创建 package.json 或使用npm install -g bar全局安装包。

You can use below command line:您可以使用以下命令行:

npm install packegName --loglevel=error npm install packegName --loglevel=error

It will only show errors.它只会显示错误。 For example, a problem in the download or the package cant be found.比如下载有问题或者找不到包。

Try this:试试这个:

rm -r -f ./node_modules/PACKAGE_NAME && \
mkdir -p ./node_modules/ && \
npm pack PACKAGE_NAME | xargs tar -C ./node_modules -xzf && \
mv ./node_modules/package ./node_modules/PACKAGE_NAME && \
rm PACKAGE_NAME *.tgz

package.json file not only contains your project information, but also contains dependencies, which makes your project self contained and deploy easily. package.json 文件不仅包含您的项目信息,还包含依赖项,这使您的项目自包含和轻松部署。 however, you can still install a package without package.json file globally by running npm install bar -g but that won't be part of you project dependency, it will be in you local machine only.但是,您仍然可以通过运行npm install bar -g全局安装没有 package.json 文件的包,但这不会成为您项目依赖项的一部分,它只会在您的本地机器中。

so, the question depends on you purpose actually, but as I mentioned, its possible to install npm packages globally without package.json file.所以,这个问题实际上取决于你的目的,但正如我提到的,可以在没有 package.json 文件的情况下全局安装 npm 包。 Just you have to install that again when you move your project to another PC or server if that is your purpose.如果这是您的目的,当您将项目移动到另一台 PC 或服务器时,您必须再次安装它。

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

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