npm install is partially cloning the source from git repo - node.js

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.

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.

git status is not showing my changed files

I have a Node.js project and have imported a few Node modules. The git_status command shows the files that I changed in the project. I have also changed some files under the node_modules directory but those are not shown. The node_modules directory is shown as untracked.
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
src/js/main-release-paths.json
I have changed only one file in node_modules:
node_modules/#oracle/grunt-oraclejet/node_modules/#oracle/oraclejet-tooling/lib/serve/connect.js
How can I track this file?
You shouldn't add the node_modules folder to git then it's managed bei the npm command. So remove that folder and use the npm install command.
When you have added something in that folder it's shown as changed, if you had first added some files. So i hop that folder is in your .gitignore file and ignored. You should check that.
Assuming you have git repo, Please check the .gitignore files inside your project. Entries in .gitignore will be ignored by Git and will not show up when you do git add.
Adding the file to track
This is most likely not a suitable approach (read below). However , if you really want to do it:
In order to track a file nested under node_modules, you should add the directory. All of its content will be staged in git
git add node_modules
Recommended approach
You probably don't want to track the contents of node_modules directory, because:
running npm install will wipe your changes
hundreds or thousand files are likely to end up present in that directory
tracking package.json and package-lock.json in git is sufficient, then populate node_modules using npm install command.
You are not supposed to modify files located in node_modules directory directly. Instead, fork the module in question, modify it, and:
either publish your own version in npm
or reference it as a git repository inside package.json (how-to)
Then, also add node_modules directory to the .gitignore file.
If the whole directory is untracked, git status will show only the directory. Because it's untracked, there is no meaningful difference to git between changed and unchanged files in there.
Other answers have already addressed that tracking node_modules is nota good idea. Of you want to track your changes to modules, consider cloning the module repo and including it as a git submodule.
It should be inside .gitignore file. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

NodeJS: installing dependencies from package.json located one level above the working folder

The project I need to work upon is structured in a way that the package.json file is one level abov the code folder. The code is located at "D:\MyNodeProj\Source\"
All the files and code is located under "Source" project including "node_modules" folder. Package.json file is located out of "Source" folder. It is located at "D:\MyNodeProj\package.json" When I try to run the project it gives an error: Cannot find module 'winston-logzio'
I know this has something to do with package.json's location as winston-logzio's entry is there in package.json
I tried npm install but it is also throwing error. It used to work before when package.json and node_modules folder were on same level. Now that package.json is one level up we are haviing all sorts of trouble.
What is the fix for this? Please note, we can not change the location of package.json now Don't know for what reason but the architect want it this way.
If you use prefix for the parent directory, you will be able to use npm install --prefix=./../ in the project folder (D:\MyNodeProj\Source\), this command will install node_modules in the parent directory(D:\MyNodeProj\). But in order to make the node_module visible to your project you can set NODE_PATH to the parent directory and then run the project.

npm module missing files after publish

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"
]

When using a private git url for an npm module, how can I configure the consuming application to only use files from the module's dist folder?

I am using angular-cli for my angular application, but because angular-cli currently does not support use for creating a library, I used the seemingly most widely used git project to create my library: https://github.com/jvandemo/generator-angular2-library
My issue is that I don't want to publish my npm module library to the public directory. Instead I want to use the git url directly in my dependencies. During development, this works fine. I can run the build locally and run an npm link inside the "dist" folder and everything is dandy. However when I push my npm module code to git, and then run an npm install in the consuming project, I'm not sure how to set it so that my consuming project just looks inside the dist folder of the module and treats it as if it was the root of the module.
For example, in node_modules/my_private_module, my file structure looks like:
dist/
-- component1
-- compoennt2
-- my_module.metadata.json
-- my_module.d.ts
-- my_module.umd.js
-- index.d.ts
-- index.js
-- package.json
-- README.MD
package.json
README.md
All the files that my application is using are in the /dist folder, but I DO NOT want to specify "dist" in all my imports like
import { myComponent1 } from 'my_private_module/dist';
I want to be able to just specify
import { myComponent } from 'my_private_module";
As I do in development when I run an npm link in the dist folder.
Is there a way I can achieve this?
Thanks
In package.json for your module, in the root folder:
typings: 'dist/index.d.ts',
main: 'dist/index.js'
Remove the package.json in your dist folder.
When the package is resolved from import {...} from 'my_private_module', the module loader will look for a folder called my_private_module under node_modules, and look either for index.js which defines the exports, or within package.json for the main property - which in your case also points to index.js from the dist folder.
It is good practice to put package.json where you want your module to be found, and have main and typings point to index.js and index.d.ts.
I answered a similar question here and it seems relevant.
Basically, treat the generated library in the dist folder as it's own repo. In order to keep the git init files and folders, you tell ng-packagr to not destroy the destination when building. Then you push the changes to the library specific repo and use that as your package url in other projects.

Resources