npm module missing files after publish - node.js

For reference, the repo is https://github.com/microsoftly/luis-response-builder.
The node module files are generated with tsc and output to the dist folder. I have a prepublishOnly step that removes the dist folder, runs tsc, then runs the test against the transpiled js. The tests pass when I publish just fine.
The problem is, when I install the project anywhere else, the dist folder contains only the file with the path dist/src/index.js.
I cannot for the life of me figure out why the file is missing when installed but not when published.

Quoting from npm-publish Documentation:
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.
Your repository's .gitignore file contains the following:
node_modules
dist
*.env
yarn-error.log
Since dist is being ignored, it's not committed with npm publish, as per the documentation.

Check out the package.json documentation about files.
Since you haven't included the files key, it will only include the file specified in main (along with some other default files).
The files value is an array so you can include multiple files and/or folders.
eg:
files: [
"dist",
"config/somefile.js"
]

Related

Why is multiple Gitignore ignored?

The folder structure is as follows.
Created for personal project.
"SERVER" directory works as a server with nodejs, and "CLIENT" directory works with React. So, if you run npm run start inside the "SERVER" folder, the server starts, and the react html generated by "CLIENT" is imported and displayed on the screen.
In git, there is a folder called "GATHER" that contains all of these CLIENT and SERVER folders.
Currently the .gitignore file is only inside the "CLIENT" folder. The contents of the "CLIENT" folder are as follows.
/node_modules
/.pnp
.pnp.js
/build
So, in "SERVER", all parts that need to be added to .gitignore such as node_modules and build are detected as changes.
To solve this problem, I added .gitignore to the parent folder of "CLIENT" and "SERVER", but it doesn't work.
So, as a result of searching, I found that it works even if there are multiple gitignores.
I created a gitignore file in the "SERVER" folder and entered the same code as "CLIENT". But it doesn't work. node_modules are still being tampered with.
I tried git rm --cached node_modules
fatal: pathspec 'node_modules' did not match any files
I only get this error. How can I solve this?
In conclusion, I want to apply .gitignore to each of the "SERVER" and "CLIENT" folders in the GATHER folder.
If your shell current working path is where the .gitignore (and SERVER folder) are, the command to use is:
cd /path/to/repo/GATHER
git rm -r --cached -- SERVER/
To remove the full SERVER content (that you want to ignore).
The OP adds in the comments:
build/ and node_modules/ (directories that should be ignored) are all being ignored.
In addition, since it is recommended to manage only one gitignore file, there were originally two gitignore files: CLIENT internal gitignore and SERVER internal gitignore.
I ended up putting one gitignore in the parent directory of these two directories.
To check if this work, you can use git check-ignore -v
git check-ignore -v -- CLIENT/build/a/file/inside
If you see an ignore rule, the file is ignored.

Ignore src and node modules in Google cloud build

I had deployed a React app to google cloud Build. Deployment is successful. But I have to hide the src and node modules directory.
Currentely Its showing
I have tried adding .gcloudignore with content
node_modules/
*~
Do you have any idea how i can hide my source files. After the build all complied code generates in the dist directory.
I am assuming that the folders node_modules and src are in the top-level directory to be uploaded, then the .gcloudignore file will look like:
#!include:.gitignore
/node_modules/
/src/
*~
The first line #!include:.gitignore allows .gitignore file, 2nd, 3rd and 4th line prevents uploading of node_modules, src directory and any files ending in ~.

npm install is partially cloning the source from git repo

When I run npm i command it partially clones the code from bitbucket repo. Here are some dependencies in package.json
"dependencies": {
"bluerain-app-hello-world": "git+ssh://git#bitbucket.org/projects/bluerain-app-hello-world.git#build_issue",
"graphql-tag": "0.1.16" }
And this is the source of my module
But when i list down cloned rope from node_module by
ls node_modules/bluerain-app-hello-world/
It gives this list
index.js node_modules package.json README.md src
There is a dist folder in my source but it didnt clone properly. I tried to remove my node modules and reinstall it but it didnt work. Any suggestions please?
I started working on a boilerplate who had added the files array field in package.json and specified only the src folder. This is why when my node module installed, it didn't contain the dist folder.
According to official documentation
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)
You can also provide a ".npmignore" file in the root of your package or in subdirectories, which will keep files from being included, even if they would be picked up by the files array. The .npmignore file works just like a .gitignore.

.npmignore not ignoring index.ts file when publishing

I'm creating an Angular2 component library I'm publishing to npm. I'm using index.ts as my barrel file throughout the source code. I've included *.ts in my .npmignore file, however, index.ts is getting included in the code published to npm. When I install the component in my consuming Angular2 project, index.ts is included in the code installed in node_modules folder. Having index.ts throughout the npm installed library is preventing the primary application from building because index.ts is not in the root folder. Surprisingly, all my other .ts files are being excluded in the npm install.
Here's my .npmignore file:
node_modules
npm-debug.log
Thumbs.db
.DS_Store
.idea
.vscode
*.ts
**/*.ts
**/index.ts
!*.d.ts
.gitignore
.npmignore
license-banner.txt
tsconfig.json
tsconfig.publish.json
Everthing is getting ignored except index.ts.
I have to manually go in and delete every index.ts file then everything works as expected.
What am I missing that's causing index.ts to be included?
I'm using npm version 3.9.6.
Thanks for your help.
After much research and trial and error, I discovered I needed to add an output directory for compiling my TS code into JS and typescript type information (.d.ts) files. Once the generated code was in a separate folder (in this case /lib), my implementation changed to import from my lib folder. All of my .ts files remained in the src folder.
My projects that have installed the library are now compiling as expected. Bottom line is to make sure your generated code (JS and .d.ts files) are in a separate folder from your source code. Seems obvious now.

Does NPM ignore files listed in .gitignore?

I have a file that is generated by npm install command (using preinstall task). I don't want to add it in the git repository, nor in the NPM project.
Supposing the file name is foo.json, I added it in .gitignore file as foo.json.
Is this enough to avoid uploading it on NPM registry?
I know I can add .npmignore file that will surely ignore the file, but I won't add it if .gitignore already does this.
If a project has both an .npmignore and .gitignore file, npm will only use the .npmignore file.
From the documentation:
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.
In simpler terms, npm prefers the .npmignore file if it is there, but will fall back to the .gitignore file.
In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignore file on its own. If there's ever a discrepancy (i.e. npm and Git need to ignore different files), then you need to maintain separate .gitignore and .npmignore files.
More information on what to put in .npmignore files: Should I .npmignore my tests?
For anyone reading this trying to ignore a file/dir from git but wish to include it in npm publish and have tried using an empty .npmignore file with no luck. This works.
In your .gitignore file, add the file/dir you wish to exclude **/build for example and in your .npmignore file make sure you specify the same file/dir but with the ! prefix so for the build example you would include !**/build

Resources