npm link only dist folder - node.js

Is there a way to restrict npm link to create the symlink only to point at ./dist?
I am struggling so far as I want to test my library (react component) locally before publishing.
In my package.json I have included the main (entry point):
"main": "./dist/index.js"
But still when I run npm link it links to the parent folder which has node_modules and so on.
This is causing problems cause now I have 2 react apps installed.
I thought of a way to exclude node_modules but I couldn't even find a way to do that.

This is actually simpler than expected. First run ng build to build your library project, then go under the dist folder and run npm link from there instead of the library root project. The symlink will be created pointing to the dist folder instead.

Using "main" parameter doesn't make sense because npm link use 'npm prefix' for create symlink
see here
You can use next hack:
Cd to project directory
Copy package.json from package to ./dist/package.json directory
Cd to dist directory
Run 'npm link'
It looks dirty but seem to only way to do it with npm link

Related

npm ERR! Could not install from as it does not contain a package.json file angular firebase functions

I am using "firebase-functions": "^3.6.0", "firebase-tools": "^8.0.0" to deploy my Angular: 10.0.14 app. All was working fine until I decided to move some components into a library created using the command ng generate library and made the package a submodule in my project under ./projects/my-lib
I build the lib using ng build my-lib
Then in my package.json installed the lib from file
"my-lib": "file:dist/my-lib"
I installed the package locally and it works fine when I test it locally but deploying using ng deploy raised the error
npm ERR! Could not install from "dist/mce-lib" as it does not contain a package.json file.
I have tried a couple of solutions I found online, but no luck yet
Try this
First, remove node_modules and package-lock.json file
rm -rf node_modules package-lock.json
Then, clean cache
npm cache clean --force
And last re-install modules
npm i
You need to create the library by using
ng generate library <your lib name>
This will setup your lib with the necessary files so that you can add it as a dependency.
Otherwise, if you want to do this manually it'll require more work.
UPDATE:
To be able to use the lib above locally without publishing you will need to do the following
From withing your app directory for which you generated the lib, go into ./projects/ as it was generated by the lib command above. Then run the following build command
ng build <your lib name> --prod
The prod tag is optional but it helps to run your code in that flag so that it is the closest to what it will look like once you publish
Then go back to your app root folder and install the application by referencing the dist folder. The build command above should have added your lib as its own folder withing dist
npm install ./dist/<your lib name>
NOTE: All of this is for dev testing purposes. Ideally, you will want to publish your lib to npmjs or github packages first and then install it properly in your app before you deploy your app to production
Another way of locally: though the use of npm link
cd ./dist/<your lib name>
npm link
Then going back to root folder of app, you should be able to run the installation as such
npm link <your lib name>
This creates a local link to your library in your app for testing purposes that doesn't modify your package.json file as installing the directory directly does. Once you are done, always make sure to run unlink
npm unlink <your lib name>
And withing your ./dist/
npm unlink
So after hours of trying to figure out how to solve this, I came across these resources that helped me to resolve the issue:
https://firebase.google.com/docs/functions/handle-dependencies#including_local_nodejs_modules_as_part_of_your_deployment_package
Note: The Firebase CLI ignores the local node_modules folder when deploying your function.
Firebase Functions local "file:" dependencies
So even if the app was working locally, the node_modules isn't deployed
Since I had installed python packages from git before, I thought why not make use of this? I am using Gitlab and this answer really helped me https://stackoverflow.com/a/50314604/3247914
I got it working by installing my package from git

npm prepare script not building folder in node modules

I am trying to use npm's prepare script to run a build step when npm installing from a different project.
The script does run during the npm install however, it doesn't build out the dist folder inside node modules.
Refer to this article for more details http://jim-nielsen.com/blog/2018/installing-and-building-an-npm-package-from-github/
I also had the same problem. My prepare script wasn't creating the build directory in the node_modules folder when installing as dependency.
Finally I found out that my .gitignore was the problem, which was setup to ignore the build directory for version control. NPM is inheriting the .gitignore file when no .npmignore can be found, which was the case here.
As stated on https://docs.npmjs.com/misc/developers:
If there’s no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file
So I solved the problem by simply adding an empty .npmignore in the root.
I hate to provide such a simple answer, but my solution was to use npm install rather than yarn, which apparently doesn't run the prepare script properly.

Running "npm install --save X" in folder without package.json file

I am writing a library and need to know what will happen if a user tries to run npm install -S X in their project before a package.json file exists.
I just tried this on Windows, and sure enough, NPM did not barf and went along with it's business but when the install command completed, there was still no node_modules folder nor a package.json file.
Does anyone know what is expected to happen? I assume I should require the users of my library to run "npm init" before running "npm install X" ?
Looks NPM does throw an error at the end of the install -
ENOENT: no such file or directory, open 'package.json'
but I wonder if that prevents the install process from create a node_modules dir, and actually putting the dependency in there.
npm install without the -g flag will walk up the folder tree checking for folders that contain either a package.json or a node_modules folder.
If either of those conditions are met, then that folder will be treated as the current directory for the purpose of the npm commands you are running. If no such folder is found, then the current folder is used.
As you noted, a node_modules folder will be created and after the package is loaded into the cache it will be unpacked into that folder.

npm not creating node_modules folder in project directory

I am doing a small Sinatra project and I want to use Gulp.
I have node(v0.12.0), npm(2.13.1), gulp(3.9.0) installed. I am in my project directory but when I try install a package like "npm install gulp-sass --save-dev", it doesn't create any "node_modules" folder in my project directory. It seems to be installing the package in my user home directory. Anything I am doing wrong?
From the npm documentation:
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.

Gulp installs outside project root

I've encountered a weird issue when installing Gulp in a new project.
Let's say I have the following path:
c:/development/myproject
When I run npm install gulp in that directory, the node_modules folder actually gets created in
c:/development/node_modules
instead of
c:/development/myproject/node_modules
And all of gulp plugins also get installed in that directory outside my project root.
I also have an earlier project where gulp was already installed before, and when I tried to rerun gulp installation in that project directory it was installed correctly in the project root (for example: c:/development/myolderproject/node_modules), not outside.
I don't think it has anything to do with the case, but the new project is using Laravel 4, while the other one is on Laravel 5.
I don't recall having to set any specific configuration before, so I'm totally confused why it behaves differently.
When you did npm install it found package.json from parent directory and thought it was the package root.
Related docs: https://www.npmjs.org/doc/files/npm-folders.html#more-information
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.
I run Ubuntu 15 and I had a similar issue where gulp was installing the node_module folder somewhere I couldn't find. gulp would say ../../node_modules was the location but it was NOT in my project folder.
I figured out from the link above and some more research I just needed to run npm init to create a project.json in my project folder. gulp was installing the node_modules in another folder because it searches for a project.json file to install the folder node_modules into.
Hope this helps anyone else solve this silly problem.

Resources