Per-project npmrc :: How do I do this? - node.js

If this is my project layout: package.json.npmrc/client/server
and I want npm to install modules to: /server/node_modules
what should I set my prefix to?
Right now my .npmrc has: prefix = ./server but with this it just installs to directory anyway and puts an empty node_modules folder in the prefix destination. What am I doing wrong here?

Nevermind. I just read this link.
Edit: It simply can't be done yet, even though in their change log it says per-project npmrc support was available in v1.4.11.
It works in the CLI , so npm install package --prefix ./folder will put node_modules folder with package into prefix destination, which isn't very useful. Using --save with that will not update dependencies in package.json, either.

Related

npm errors and warnings in terminal

can anybody tells me what are those errors about and how to fix them?
Everytime i try to install something in node with npm these errors show up in terminalenter image description here
ENOENT stands for basically "Error, No Entry". this means it was looking for the package.json file and it couldn't find it.The fields mentioned below are also not found because they are a part of the package.json file
so create a package.json file in the current current directory using
npm init
and add the required content in the required fields
i would also recommend you to install the modules locally into the directory of the particular project using
npm install express --save
Hope my answer helps,
cheers
The error you are facing is because you do not have package.json file. Npm installs the package in a node_modules/ subfolder, but warns you that there is no package.json file. If you want to manage localy installed npm packages you should create a package.json file. Start by creating an empty folder:
$ mkdir myapp
$ cd myapp
and then create a new package.json executing
$ npm init
Answer (or skip) all questions and at the end a brand new package.json will be created.
You can get more information from the Getting started articles in npm documentation: Working with package.json

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.

npm install local folder only installs node_modules

I want to install a local project by using "file:../project-name" in the package.json. However, when I run this command it only installs the dependencies of the local project (node_modules) and not the source files itself.
I have project-name/src for example and I expect it to appear in node_modules/project-name/src, but it doesn't.
I checked the .gitignore (I don't have a .npmignore file) and src as a folder is not excluded.
What am I not seeing here?
If you refer to the files property, this does not indicate files to copy to the node_modules on npm install, but it indicates the files to publish on npm publish.
Edit
When including a dependency with file:... in a project, the directory has to:
have a package.json
Declare exported files in the "files" property.

Add local project dependency for npm install

What is the proper syntax to add local project dependency in npm package.json file?
I have git project locally in C:\projects\MyApp
I want to get this project with npm install. I tried following
"dependencies": {
.....
"my-app": "file://../projects/MyApp/MyApp.git"
.....
}
but getting error
Could not install ....
Any suggestion?
Finally got it working
"my-app": "../projects/MyApp"
Its' simple until you know.
Local dependency has to be a directory on your filesystem.
Alternately there is npm-link.
Excerpt from the docs:
Package linking is a two-step process.
First, npm link in a package folder will create a globally-installed
symbolic link from prefix/package-name to the current folder (see
npm-config for the value of prefix).
Next, in some other location, npm link package-name will create a
symlink from the local node_modules folder to the global symlink.
Example:
cd ~/projects/node-redis # go into the package directory
npm link # creates global link
cd ~/projects/node-bloggy # go into some other package directory.
npm link redis # link-install the package
File is the wrong protocol. You can use git+ssh or git+https.
Here can you find more information about your question:
https://stackoverflow.com/a/10391718/5111420
and I see a typo: dependencioes -> dependencies

How can I change the cache path for npm (or completely disable the cache) on Windows?

I've installed Node.js on my Windows 7 x64 development machine, the manual way:
mkdir C:\Devel\nodejs
cd C:\Devel\nodejs
set NODE_PATH=%CD%
setx /M PATH "%PATH%;%NODE_PATH%"
setx /M NODE_PATH "%NODE_PATH%\node_modules"
I've placed the main node x64 binary along with npm package manager in C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.
The only problem I can't solve is moving the cache folder. When I install a local package:
npm install express
... cache is placed under %APP_DATA%\npm-cache folder. I'd like to change it to:
C:\Devel\nodejs\npm-cache
How can I change the npm cache folder, or disable it completely?
You can change npm cache folder using the npm command line. (see https://docs.npmjs.com/cli/v6/using-npm/config#cache)
So you might want to try this command :
> npm config set cache C:\Devel\nodejs\npm-cache --global
Then, run npm --global cache verify after running this command.
You can also set an environment variable with export npm_config_cache=/path/to/cache (Unix) or set npm_config_cache=C:\path\to\cache (Win) as an alternative to npm config set (this is true for all config options in npm).
For anyone using docker you can add the env var at runtime with:
docker run -e npm_config_cache=/path/to/cache mydockerimage:tag
You can also do following:
For having cache path as you wish, for a single package while installing it:
npm install packageName --cache path/to/some/folder
For having cache path as you wish, for all the packages in package.json:
Just be in the directory where package.json is as usual and do
npm install --cache path/to/some/folder
You may not find this in npm documentation but i have tried it with npm 6 and it works.
Looks like it works since npm 5 [Refer: How to specify cache folder in npm5 on install command?
In Windows you can simply cd to the desired cache folder and do npm set cache --global
Solution
Paste the following code into npmrc file.
Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc
prefix=D:\nodejs\npm
cache=D:\nodejs\npm-cache
Notes:
There is no '.' in front of npmrc
Diagrams
NPMRC file folder look like this
NPMRC Content look like this
Hope it helps. Cheers
In addition, I found that running an update command works also - for example:
npm update npm
Lastly, one can check their npm-cache directory to see if is being filled or not.

Resources