I accidentally changed the npm prefix to a place that doesn't exist. Is there a configuration file I can access that would allow me to change this back?
The only options I can think of are:
Completely Uninstall Node (npm not responding after changing the prefix)
I guess I could create the directories that don't exist and move the npm files there.
But it seems like there should be a config file I can change somewhere, right?
prefix can be defined per-install and other commands using --prefix, but as a global setting it is in ~/.npmrc (C:\Users\<your user name>\.npmrc). You can remove / edit it directly in that file.
You can also use npm config set prefix $value, or npm config delete prefix if you prefer.
A Simple ln -s /usr/local/bin/n /usr/bin/n has fixed the issue.
Related
In Windows, I want to change the default global npmrc location, how can I do this?
I ran npm config set prefix C:\npm to change the prefix of the global npmrc location.
And then npm config get globalconfig to get the exact expected global location (which is C:\npm\etc\npmrc).
I navigated to this location and defined my global npmrc there (I had to create the etc folder myself.
I have to work with some packages in the private registry. So, in my package.json in the dependencies section I have a lines like this one:
...
"dependencies": {
"#myco/my-awesome-package": "^0.4.5",
...
}
...
There is authentication required for the private registry, so I have to create the .npmrc file in my project:
registry=https://registry.npmjs.org/
#myco:registry=https://myco-registry-path/
//myco-registry-path/:username=${MYCO_REGISTRY_USER}
//myco-registry-path/:_password=${MYCO_REGISTRY_PASSWORD_BASE64}
Yes, I know about _authToken, but in my case it is easier to use user and password.
Anyway, here you can see two env variables: ${MYCO_REGISTRY_USER} and ${MYCO_REGISTRY_PASSWORD_BASE64} which I have to replace before npm install.
I know the very simple solution for this problem: put them to the "global" env variables for example to my .bash_profile (or any terminal profile of your choice).
But I do not want to keep variables like this in the "global" scope because the are important only for the current project. What I want to do is to use dotenv. I want to create a .env file in the root of my project:
MYCO_REGISTRY_USER=myco-registry-username-value
MYCO_REGISTRY_PASSWORD_BASE64=myco-registry-password-value-base64
I want that this values replace env variables in my .npmrc on the install action. But when I try npm install I get an error: Error: Failed to replace env in config: ${MYCO_REGISTRY_USER}. I can understand why it happens. Possibly because npm reads .npmrc values first and try to replace env variables and fails, because in this moment it know nothing about dotenv.
My question is how to deal with it?
Short summary:
I do not want to keep env variables in the terminal profile, instead I want to put it in the .env file inside my project.
I have to replace env variables in the .npmrc file with dotenv before npm install
I know this answer might come too late, but in case anyone else is looking for answers, here's a solution:
You need to prepend your scripts with dotenv-cli as so:
dotenv npm install
or in my case where the file was not .env:
dotenv -e .env.local npm install
The problem is that you cannot save this anywhere so that someone can use it with "npm install" somehow. Definitely npm preinstall is run after reading .npmrc so it fails too.
You will need to either document it well or just include a small shell script, but if you're supporting different OSs then it can get funny really fast...
Happily so, CD platforms like Netlify allow you to set environment variables manually.
But I guess this must not be the nicest of starts if someone clones your repo and the first they've got is a failing npm install 🤷♂️
Also, check this one out: locking-the-vault-on-font-awesome-npm-tokens
Is it possible to change where you install your dependencies when doing npm install -g module? I know it installed in your C:/../{name}/Appdata..etc but I want to change the path on mine due to limited disk space.
I've installed node.js on an external disk which is fine and can do npm commands, but now I want the global dependencies to be installed on this disk as well.
Is there a way to do it?
You can configure it to new PATH by the following command -
npm config set prefix '~/.npm-new-global'
You can also use the environment variable NPM_CONFIG_USERCONFIG to set a new local for your config file, that is, npmrc. For instance, you can add to you login config file (~/.profile) the following line
export NPM_CONFIG_USERCONFIG="$HOME/.config/npm/npmrc"
Then, on the file ~/.config/npm/npmrc you can write
cache=~/.cache/npm
prefix=$~/.local/share/npm
Here, the advantage is that you have set both the ~/.npm and its cache to match with the XDG Base Directory Specification
reference
Does anyone know how to configure location of global repository?
My global repo is somewhere under $HOMEDRIVE/$HOMEPATH/blahblahblah
and all my packages are being installed under that place fopr global reference
but I want to park it somewhere specific and secret like the docroot of my appserver ? so I can operate demos and proof-of-concepts and prototypes ands show them off
can you tell me how I can configure the path to my global repository? I am on windows7 which is thoroughly supported and chmod chown issues are not as prevalent on linux
is this directory anchor controlled by a designated variable within NPM?
is this variable ever referenced by javascript modules indiscriminantly? i would hope not
I assume this variable is within the NPM tool itself.
what about bower... would bower operate the same configurable? or is bower a different animal and place.
is bower a subset of npm? anmd of so does it operate the same configuration as npm?
thank you
See the npm docs about the folders. It states that the global modules are installed under a configured prefix. You can get it from the npm config comand:
npm config get prefix
And you may change it with a similar command:
npm config set prefix /path/to/my/global/folder
However, modules are usually installed globally if want to use some command line command they provide. For using in some node.js application, prefer to install them locally. If you still want to use the globally installed modules inside the application, you should use the link command (though I'm not sure if it works in a Windows environment).
Bower is another thing completely. Looking at the api documentation, you will see that there is no option to install modules globally (which makes sense, as Bower is intended for front-end dependencies).
You could change the default folder using the directory parameter of your .bowerrc file (see the documentation). This way you would be able to set all projects to use the same folder, but notice that's not the way it's intended to use and you would need to set it in all projects.
npm config set registry <registry url>
once this command is run, check in ~/.npmrc, it must show your changes.
I am trying to publish a pure client project - that is - plain html/js/css files, that during dev being managed by nodejs.
Node creates a very deep path (longer than 260 chars) - inside node_modules/...
Although I have excluded node_modules completly:
<ExcludeFoldersFromDeployment>test;node_modules</ExcludeFoldersFromDeployment>
It still throws an exception when I try publishing:
Error 1 The "CollectFilesinFolder" task failed unexpectedly.
System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified
file name must be less than 260 characters, and the directory name must be less than 248 characters.
at System.IO.PathHelper.GetFullPathName()
I know it is specifically the node_modules, because manually removing it solves the issue.
Other then that, excluding works fine (the 'test' folder is being excluded).
How do I keep these files from being considered for the publish?
I solved this by installing some (if not all) of the dependecies globally:
npm install -g package
and then installing into the project using
npm install --link
The result is that NPM will create shortcuts to your globally installed packages instead of copying them, and it seems that msbuild does not try to follow these links.
You can simply hide the folder, i.e. set the hidden attribute, and aspnet_compiler will skip it avoiding this error to happen.
I've tested this with TypeScript typings, and grunt tasks, and they work perfectly with this folder hidden. Of course, npm install and npm update also work fine.
If you hide this in the command line or using a script, use this command
attrib +H "path\to\folder\to\hide"
You can also use this command in a msbuild task, as explained in this answer to How do you handle excluded files with aspnet_compiler?