7
votes

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.

Is there a bonafide reliable way to install a depedency into a directory if there is no package.json file there (yet)?

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'

I guess my question then becomes, is there a way to tell NPM to ignore a missing package.json file?

5
@stdob-- so npm pack foo is the way to go?Alexander Mills
Some like this: rm -r -f ./node_modules/PACKAGE_NAME && mkdir -p ./node_modules/ && npm pack PACKAGE_NAME | xargs tar -C /var/www/node/ork/node_modules -xzf && mv ./node_modules/package ./node_modules/PACKAGE_NAME && rm PACKAGE_NAME *.tgz :)stdob--
add it as an answer and I will upvote, seems to work for meAlexander Mills

5 Answers

11
votes

Deleting package-lock.json solved for me.

3
votes

One of the primary reasons for a package.json is to document the dependencies that a given application has. 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.

1
votes

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
1
votes

You can use below command line:

npm install packegName --loglevel=error

It will only show errors. For example, a problem in the download or the package cant be found.

-2
votes

package.json file not only contains your project information, but also contains dependencies, which makes your project self contained and deploy easily. 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.

so, the question depends on you purpose actually, but as I mentioned, its possible to install npm packages globally without package.json file. Just you have to install that again when you move your project to another PC or server if that is your purpose.