npm publish is renaming .gitignore to .npmignore - npm

I've created a Yeoman generator to create a basic Git repository folder structure. One of the assets is a .gitignore file. This is in the \generators\app\templates\ folder.
When I publish this generator to my NPM registry, the .gitignore file is renamed to .npmignore and then the generator fails as it's trying to copy a file that no longer exists.
How can I stop this happening?

What we ended up doing is renaming the .gitignore files to dot-gitignore and renaming them after the generation :
in index.js :
// called at the end by yeoman
end() {
extfs.copyFileSync('_gitignore','.gitignore);
extfs.unlinkSync('_gitignore');
}

Related

npm pack doesn't include all files in the package

I run npm pack inside dist folder, but it doesn't include all it's files.
it just contains: package.json, ReADME.md, index.js (main).
there is no .gitignore file, and I tried to create an empty .npmignore file, but it didn't help.
I also tried to add files array to package.json.
all files in the root or subfolders (js or other files) don't included in the pack file.
I found another files property in my package.json, so adding another one has no effect, also .gitignore and .npmignore have no effect.

npm pack/publish won't pack .gitignore or .npmrc files

I have built a project generator for my company. It's a globally installed npm package, that when run, takes the entire contents of a /template directory inside the package and copies it to the user's chosen destination.
Inside /template I have 2 files that npm pack refuses to include in the final published module:
/template/.gitignore
/template/.npmrc
Everything else, including other hidden files, get packed as expected.
These 2 files are not in any root (or nested) .gitignore files, and I'm not manually specifying any files array in any package.json that npm might pick up on.
This is intentional behaviour https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package
The workaround was to create .gitignore.template and rename on install
Explicitly adding to files in package.json will force npm pack to include the files
"files": [
"dist",
"dist/template/.npmrc",
"dist/template/.gitignore"
]

Forking and changing an NPM package

I have been using an NPM for angular-4 which support drag and drop objects (ng2-drag-drop). I found a missing functionality and decide to add it to the package.
What I did is forking the original project and adding my changes. after commit/push to my git I then used the following command to install my NPM :
npm install https://github.com/..... --save
the NPM installed successfully however when looking in my node_modules I see that the source files are missing and I have only the root directory including the package.json and some other files . any source files are missing.
I then tried to instal the NPM directly from the author git so instead of running :
npm install ng2-drag-drop --save
I used
npm install https://github.com/ObaidUrRehman/ng2-drag-drop.git --save
and I had the same issue with my fork.
Why the installation is different between the author git and the named package ? isn't it taking the files from the same location ? if no, what should I do to make it work ?
The reason you are not able to see the src folder is
If you see the git repo you will find two files
gitignore & npmignore.
In that npm ignore file you will find the src has been ignored to be prevent it from being added to the package when running npm commands .
Keeping files out of your package
Use a .npmignore file to keep stuff out of your package. If there's no
.npmignore file, but there is a .gitignore file, then npm will ignore
the stuff matched by the .gitignore file. If you want to include
something that is excluded by your .gitignore file, you can create an
empty .npmignore file to override it. Like git, npm looks for
.npmignore and .gitignore files in all subdirectories of your package,
not only the root directory.
You need to overwrite these settings to be able to get src contents in node modules when you do npm install

How is the shrinkwrap file generated?

When locking down node modules using shrinkwrap, how is the npm-shrinkwrap.json file generated? Does it recursively check node_modules folder and get it's info there?
I'm asking because I did a npm install successfully behind a proxy which translated github repo's to an internal artifactory repo, but the generated shrinkwrap contains urls directly to github (what I don't want)
write npm shrinkwrap in your terminal folder which contains package.json file which will genrate npm-shrinkwrap.json file
Ya this will check recursively in your node module folder of directory.

npm publish isn't including all my files

I npm publish'd a module. It went up fine, but then when I installed it from the registry, it turned out to be missing certain files.
When I run irish-pub in my module's project directory, sure enough, the output doesn't list those filenames.
I've checked:
I do not have an .npmignore file.
I do have a .gitignore but this only contains /node_modules/
the missing files are normal JS files, not things that could be ignored by default
What else could be blocking them?
The problem was I had a files array in package.json. If this is present, then only the specified files get published, and everything else is effectively npmignored.
https://docs.npmjs.com/files/package.json
i've had the "files" property in package.json as well (intentionaly) but used relative pathes to the list of files and directories starting with dot slash ("./"), but neither npm pack nor npm publish worked with that. removed these, all worked as expected!
so change:
"files": [ "./bin", "./lib" ]
to:
"files": [ "bin", "lib" ]
and run:
$ npm pack
check the gnu zipped tarfile and finaly:
$ npm publish <projectname>-<semver>.tgz
For anyone not fixed by the above answers, npm pack and publish respect any package.json files you have in the directory tree, not just at the root.
In my case, I had a module that templated out another module with ejs, but because npm was treating the child package.json as real it was reading the files from there and not bundling everything I needed.
Lookout for the files in any package.json, not just your root.
For me, having the .gitignore file with files listed in it, inside the package folder to be published was causing the issues.
In general,
"All files in the package directory are included if no local .gitignore or
.npmignore file exists. If both files exist and a file is ignored by .gitignore
but not by .npmignore then it will be included."
I just ran into the same problem and found the answer here.
You need include the path to the directory (or tarball) you're trying to publish. While the documentation on npmjs.org doesn't really indicate it, if you run npm help publish you'll get the man page, which shows that the correct usage is actually
npm publish <tarball> [--tag <tag>]
npm publish <folder> [--tag <tag>]
I also found that I had to actually type out the path: I couldn't just use npm publish . from the directory containing my package.json file.
Hope that helps.
Something not mentioned in other solutions is that there is an undocumented, racing precedence. For instance, I had "files": ["lib"] in my package.json. lib is my gitignore. with just that state, it works. however, there was also a lib/path/.gitignore, which trumped my files array, yielding no included lib folder.
lesson--take heed of nested .gitignore files