Run "npm install" as if package was not in workspace - node.js

I am working on an NPM workspace node project. To deploy one of the workspace's packages, I would like to run npm install and obtain a node_modules directory as a subdirectory of that package such that the package becomes self-contained.
Consider the directory structure below:
node_modules
packages
├ cloud-app
│ ├ src
│ └ package.json
├ helpers
│ ├ src
│ └ package.json
├ business-logic
│ ├ src
└ └ package.json
package.json
Just one deduplicated node_modules is excellent for development in a monorepo. But to deploy the cloud-app package, I need the structure to look like this:
packages
├ cloud-app
│ ├ node_modules
│ ├ src
│ └ package.json
├ helpers
│ ├ src
│ └ package.json
├ business-logic
│ ├ src
└ └ package.json
package.json
Then, I could upload the cloud-app directory as usual without exposing my NPM workspace to the vendor's (incompatible) CD pipeline.
Is this possible at all? What would be the correct command or procedure here?

This seems to work. Try the workspaces argument:
cd <my_project_root>
npm i --workspaces=false
https://docs.npmjs.com/cli/v9/commands/npm-install#workspaces

While I did not find a standard way to achieve this, there is a slightly hacky way that worked for me:
Copying the node_modules directory allows the package to act as a stand-alone module. However, there is one caveat: The node_modules directory contains a symlink for each package in the workspace. Thus, a loop begins when it is copied into a package and when symlinks are followed. To prevent this, we first have to delete our package. Therefore, a deploy script could look something like this:
rm ./node_modules/cloud-app
cp -rL ./node_modules ./cloud-app/node_modules
# deploy cloud-app here
I thought of this while formulating the above question but would still be delighted to know whether there is any canonical, supported way to do this.

Something you can try is what #youzen brought on this post.
Basically, in the root folder of your repository, you can run:
npm install --prefix <path/to/prefix_folder> -g
And so, the node_modules folder will be created in the specified folder.
You could access the link mentioned above to get others solutions, hope this helps!

Related

NPM: how to publish a npm module with local dependencies in a monorepo?

I have a monorepo architecture that can be simplified like so:
bootstrap/
├── module1/
│ └── package.json # uses "module2" package
└── module2/
├── global.d.ts # contains global types that module1 needs
└── package.json
So, I want to publish module1 without publishing module2.
Actually, the module2 code is not embedded when publishing. When I copy the files somewhere, I have a conflict because the global type file is defined twice.
Is there a way for npm to handle that? Or any workaround?

Debug npx environment selection for nested directories?

I have nested directories with different nodejs environments (package.json and node_modules/), and need to run npx under different environments. Something like this:
a/
├─ node_modules/
├─ b/
│ ├─ c/
│ │ ├─ node_modules/
│ │ ├─ package.json
├─ package.json
Recently, I created directory c/, and found that when running a command with npx under c/, it shows some warning related to a/node_modules/. Both environments have this command (package), to avoid confusion.
Thus, I would like to check why npx is unexpectedly looking at the upper levels of the directory structure.
Is there a way to output the relevant information?
For example, how do I check which (environment of) directory that npx is running commands in?

Does ESLint look up the directory tree in order to find an extended config?

Say you have the eslint-config-next package installed at the root of a monorepo, and you have a base ESLint config at the root which is extended by a child ESLint config in a sub-package:
# monorepo
├── .eslintrc.base.yml
├── apps
│ └── client
│ └── .eslintrc.yml
└── packages
└── components
└── .eslintrc.yml
Here's how the child .eslintrc.yml in packages/components config looks like:
extends:
- "../../.eslintrc.base.yml"
- "next"
But there is no node_modules directory in packages/components. Does ESLint look up the directory tree (in this case, go up two directories at the root of the monorepo) to locate the next config in the node_modules directory?

ERROR with sharp dependency when deploying nodejs app to AWS elastic beanstalk

i am a beginner in AWS, and I encountered this problem very early on.
I created an EB environment and a code-pipeline in AWS. So whenever I push something to the repository, the app gets deployed. So for now I just have a "Hello world" node.js app, but I want to install the sharp npm dependency for later on. When I put the dependency in the package.json file and push it to the repo, a get the following error:
error on deployment.
I have done a lot of googling, and I think it has something to do with setting permissions to install the sharp dependency. However, none of the solutions I found have worked so far.
If anything is unclear, I apologize and let me know :).
Please reference my "workaround" solution provided in the following GitHub Issue (Fails to install on AWS ElasticBeanstalk with node16 #3221) for a full explanation.
Solution:
Create the following Platform Hooks paths in the root directory of your application bundle.
.platform/hooks/prebuild
.platform/confighooks/prebuild
Create the following bash script (00_npm_install.sh) with execute permissions (chmod +x).
#!/bin/bash
cd /var/app/staging
sudo -u webapp npm install sharp
Validate the Application Bundle Structure.
Ex. Sample project structure:
~/my-app/
├── app.js
├── index.html
├── .npmrc_bkp
├── package.json
├── package-lock.json
├── .platform
│ ├── confighooks
│ │ └── prebuild
│ │ └── 00_npm_install.sh
│ └── hooks
│ └── prebuild
│ └── 00_npm_install.sh
└── Procfile
Deploy the Application!
Hope it helps!

Netlify dont deploy specific folder or files

I have a git a repository set up with CD on Netlify. The site itself is 4 files, but I have some other files I'd like to add to the repository that I don't want deployed. Is there a way to deploy only certain files with a deployment? Or only a specific folder?
My site only requires an http server, there's not an npm, jekyll, or hugo install. It's just the deployment of 4 files.
If you put the files in a specific folder you can set the Base Directory in your Build & Deploy settings to that directory and it will ignore the other files/folders not in that directory.
According to the docs, this is a bit more involved than just setting the base repo:
For this next example, consider a monorepo project set up like this, where blog-1 is the base directory.
repository-root/
├─ package.json
└─ workspaces/
├─ blog-1/
│ ├─ package.json
│ └─ netlify.toml
├─ common/
│ └─ package.json
└─ blog-2/
├─ package.json
└─ netlify.toml
The following ignore command example adapts the default behavior so that the build proceeds only if there are changes within the blog-1 or common directories.
[build]
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../common/"

Resources