npm choose global install directory - installation

I want to install npm dependencies from a package.json in the current directory, but I want to install them to a different directory.
Can I make this setting (install directory of node_modules) project specific, so as to not interfere with my other projects?

You can try --prefix.
npm install --prefix new/path/node_modules
https://docs.npmjs.com/misc/config#prefix
NOTE: Important, that there is a node_modules folder

Related

Azure pipelines working directory pointing two different places

Hi Everyone I have a question related to the pipeline.workspace variable.
In the example below i have set pipeline.workspace as the working directory and as paths for a cli command.
Npm install creates folders under /home/vsts/work/node_modules/
while the next command when i use pipeline.workspace it points to ./home/vsts/work/1/
Am I doing something wrong? or is something up?
- task: Bash#3
displayName: 'Publish Sentry'
inputs:
targetType: 'inline'
script: |
npm install #sentry/cli
.$(Pipeline.Workspace)/node_modules/.bin/sentry-cli releases --org --project new "$(Build.BuildNumber)" --finalize
workingDirectory: "$(Pipeline.Workspace)"
The node_modules folder is pre-generate that may contains some global packages. It is not generated by the npm install command in the script. This situation exists on Microsoft-hosted Ubuntu agents and Microsoft-hosted macOS agents.
When executing the npm install command to install packages locally, there are few points you need to pay attention to :
If no node_modules folder is existing in current working directory, and also no node_modules folder is existing in any parent directory of current working directory, the npm install command will generate the node_modules folder in current working directory and install the packages into this node_modules folder.
If no node_modules folder is existing in current working directory, but the node_modules folder is existing in parent directory, the npm install command will install the packages into the existing node_modules folder in the closest parent directory.
For example, there are the following paths:
/root/dir1/node_modules
/root/dir1/dir2/node_modules
/root/dir1/dir2/dir3
When executing the npm install command in the directory "/root/dir1/dir2/dir3", the packages will be installed into "/root/dir1/dir2/node_modules".
If the node_modules folder is existing in current working directory, regardless of whether the node_modules folder is existing in parent directory or not, the npm install command will install the packages into node_modules folder in current working directory.
For example, there are the following paths:
/root/dir1/node_modules
/root/dir1/dir2/node_modules
/root/dir1/dir2/dir3/node_modules
When executing the npm install command in the directory "/root/dir1/dir2/dir3", the packages will be installed into "/root/dir1/dir2/dir3/node_modules".

How to install yarn workspace packages without symlink?

I have a yarn workspaces project which looks something like this:
node_modules
packages
shared
test.js
package.json
client
test.js
package.json
server
test.js
package.json
package.json
server.Dockerfile
As you can see, I have a server.Dockerfile, which builds an image of the server that I can push up to different hosting providers such as Heroku or AWS.
I copy packages and package.json into this container:
COPY packages packages
COPY package.json .
And I then install only the dependencies for the server package:
RUN cd packages/server && yarn install
All the dependencies are now in the node_modules folder, and the next thing I think of doing is to delete the packages folder to remove any unnecessary code from the docker image (e.g. the client code):
RUN rm -rf packages
The problem with this is that all the yarn workspace packages inside the node_modules folder are simply symlinks to the packages folder... so I cannot delete that folder.
How do I get yarn install to make a copy of the yarn workspace packages instead of creating symlinks?
Or, is there another way to remove all of the unused code (e.g. the client code) so that my docker image isn't bloated?
You can use yarn-workspace-isolator to extract the package with its local dependencies to avoid publishing them to npm if you don't want to.
isolate-workspace -w my-package -o ~/dist/my-package
Now, as the doc saying:
You can simply run yarn install inside of ~/dist/my-package and yarn will
install all dependencies as if you had not used workspaces at all
without having to publish any workspace dependency.
Running yarn install in workspaces does the same thing inside any package or the root directory. It installs the modules for every package and symlinks them etc.
If you want to build a docker image for just the server you should only copy that package into the container and install that as an independent package.
If the server has a dependency on the shared lib, you could publish it to npm so it can fetch it too.

Where does "npm install -g <package>" install the package? [duplicate]

This question already has answers here:
Where does npm install packages?
(23 answers)
Closed 3 years ago.
I have installed some packages using npm install -g and was wondering were they were actually installed?
If I use npm install they are installed in the current directory under node_modules it appears.
I understand how to use the -g flag from (when to use) and (how to use).
When you do npm install -g <package>” then it means you want to install that particular npm module system wide (globally). And when you do without the g option, then module would be installed locally inside the current directory called node_modules.
The -g(global) directive tells npm to install the package in the global shared node_modules folder and usually you get that where nodejs resides(Path: C:\Program Files\nodejs).This will also allow you to access the module from the command-line at global level.
You can also check where global packages/libraries are installed:
Run below command
npm list -g or npm root -g
In windows:
C:\Users\admin>npm list -g
C:\Users\admin\AppData\Roaming\npm\node_modules
In mac: you find node_modules here
/usr/local/lib/node_modules
Whenever you put -g then it installed that file globally and that can accessed in any folder you wants then you don't need to install again again same package.
For more details.
You can also check where local packages/libraries are installed:
run npm list or npm root
And, when you simply install then that particular package restricted to that folder.
So, whenever you package dependencies at global level then you need to put -g while installing folder.So,
If you’re installing something that you want to use in your program,
using require('whatever'), then install it locally, at the root of
your project.
If you’re installing something that you want to use in your shell, on
the command line or something, install it globally, so that its
binaries end up in your PATH environment variable.
check this.
Hope it helps.Thanks

Polymer installation on windows

Has anyone installed Polymer on windows? I am new to using GIT and it says you need Bower and Node and some other things to get it to work.
We are working on a simple ZIP file for people to download and get started, but we haven't finished that yet.
Until we get that ZIP done, you will want to get started by installing Bower. Bower helps you manage component installations. You can worry about GIT later.
Install Bower:
Install nodejs
execute this command:
npm install -g bower
Make a project, acquire some Polymer elements:
make a project folder
inside project folder, execute this command:
bower install Polymer/polymer-ui-elements
That will produce a number of components in your project folder to help you get started. Please refer to http://polymer-project.org for more information.
Step 1:- install node.js
Step 2:- install Git
set the Git location Path (system's environmental variable)
like:-- D:\Program Files\Git\cmd
Step 3:- install bower using npm install -g bower command check the location of npm global binaries by using command (npm config get prefix).
copy the Path and set the bower location Path (system's environmental variable)
like--> C:\Users\USER_PROFILE\AppData\Roaming\npm
the whole Path will look like..(node to Polymer)
D:\Program Files\nodejs\;D:\Program Files\Git\cmd;C:\Users\USER_PROFILE\AppData\Roaming\npm
Step 4:- using command prompt go to project folder or create a project then type
bower install polymer
inside project
folder.

install npm module in my project

I have node.js project on drive "F".
for example F:\development\WebStormProjects\test-app\
so, how i can install nmp module (npm install twitter-bootstrap-node) in that project folder?
my os is windows.
Thank's in advance.
You can either add the module as dependency to your package.json or install it manually. In any case, typing above command in the command line while being in your project directory should have the desired effect.
If you've added a module as dependency then a simple npm install from the project root should suffice.
EDIT: Global installation of modules is not the default, if you were worried about this. You have to actively trigger it with npm install -g $MODULE_NAME

Resources