How to install npm dependencies inside a sub directory? - javascript

I have a package.json in the root: "install": "cd packages/my-package && yarn".
When I run yarn run install all I'm asking is that it goes into this package, installs the node_modules and dependencies and that's it. For some reason no matter what I try (yarn workspaces/lerna/manually etc.) it is installing node_modules in all packages, including the root.
WHY?! Just let me install them separately :(
Anyone knows how to easily fix this?

To move the basefolder of node_modules, you will have to create a .yarnrc file in the same directory as your package.json file. Your .yarnrc file should look like
--modules-folder packages/my-packages
The resulting output will put all of your packages after my-packages
/packages/my-packages/WHERE_YOUR_PACKAGES_GO

Related

npm start and install are not working

I tried to run npm install and npm start but both does not work. I reinstalled the nodejs but still the same. How to resolve it?
If you're trying to install the dependencies of an existing project (which the context implies), you need to run the command npm install in the directory where the package.json file of the project is.
If you've created a new and empty project folder, you need to first run npm init to create a package.json file which is used to manage the dependencies of your project.
Are you sure you're in the head directory? For example, if your project is in:
$ Documents/Projects/JS-Projects/Work/Client-Management-App/
Then that's the folder you need to be in: the Client-Management-App/ directory, because that's where the package.json file and the /node_modules/ folder will be created.
Also, have you run npm init? That might be another reason why.

Npm module not found

I'm running an Angular app built with Grunt and using Bower and NPM.
I tried installing my npm module locally. The files are in the main app directory in node_modules folder.
The module docs ask me to load the module with <script type="text/javascript" src="node_modules/moment/moment.js"></script>, but I get 404.
Am I missing something? Do I have to tell Grunt that I want these NPM modules?
Can you provide more information on what your app is built with? If node serves your app, you need to make the directory you link to public. Assuming you're using express, this would look something like this in your app.js file:
app.use('/node_modules', express.static(__dirname + '/node_modules/moment/moment.js'));
Edit:
Or if you just want to make it work, try to load moment.js from CDN like this:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Link to moment on CDN
Basically, npm is the package manager for all the javaScript frameworks such as Nodejs, angularjs etc. npm should be installed globally in the machine.You can install it from https://nodejs.org/en/ .
Next,you need check for the package.json file in your project.
If there is a package.json already existing in your project folder, then from command line you need to go to your project folder and type npm start.
If package.json file does not exist, then in the command line type npm init,then there will be a package.jsonfile created in your project folder.Then edit the package.json . and add the node packages into the package.json as similar way to this
{
"name": "shoppingkart",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www" //If you have any scripts.
},
"dependencies": {
"mongoose": "^4.9.0", // here you should have all your node_modules listed
"passport": "^0.3.2",
"stripe": "^4.15.1"
}
}
if you are not able to add the dependencies to json file, there is also another way to do it.
just go to your project directory in the command line and type
npm install --save grunt // And you need to do for all the node_modules, by replacing the **grunt**.
Automatically the dependency will be added to your package.json file.
If you installed your npm packages locally then your node_modules folder should found at the root of your project.
If you installed all your packages globally you may not see an npm_modules folder in your project.
To see where your global modules are located you can run
npm list -g
I faced the same issue just install the package globally and save at the end.
Like:
npm install -g <package> --save
Even the above doesn't work then use -f / --force at the end to force install.

node.js / npm - override local default modules directory (node_modules)

Let's say I don't want to install my local (per project) packages in node_modules - I'd like to have it under sources/node_modules instead of just node_modules. Is it possible to override it just like you can do in bower?
In bower you provide .bowerrc file with directory option, see docs. It works exactly as if you had bower_components locally - no additional files, loaders, nothing at all - just the container dir is different.
tl;dr
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources/
$ export NODE_PATH="`pwd`/sources"
Explanation
You can (as #adeneo mentioned) simply install packages to any folder with:
$ npm install --prefix sources/ my-package
This will install to sources/my-package. However, this solution is far from neat in two ways:
package.json
It sounds as though, rather than installing individual packages, what you really want to do is install everything in package.json to the sources/ folder. The problem is that when you do npm install --prefix sources/, it also looks for package.json in the sources/ folder. If that works for you, then great.
The only way I've found to keep your package.json in your project root and install node modules somewhere else is to symlink the local package.json into that directory:
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources
require
As you mentioned, you will probably want your scripts to be able to require modules like normal - require('my-module') rather than require('sources/my-module').
The NODE_PATH environment variable can help here:
$ export NODE_PATH=`pwd`/sources/node_modules
$ node -e "require('my-module')" # Success
There is an important caveat: node will look for modules in a specific order:
In a node_modules folder in the local directory
In a node_modules folder in the parent directory, then its parent etc.
In paths in the NODE_PATH, if it hasn't found the module yet
So be careful there are aren't any other node_modules folders in the current or any parent directory which mention your modules, or things could get quite confusing.
Create local_modules in your root directory and add package.json "#amazon/product-module": "file:local_modules/#amazon/product-module",
file structure
local_modules
#amazon (folder)
product-module (folder)
[add your file which overrides]
Now do npm update and you will get your local_modules/#amazon/product-module file into node_module/#amazon/product-module

"npm install" is not downloading all files from the "babel-core" package, but it does if I specifically install "babel-core". Why?

This is my package.json.
If I do an npm install from the root folder, it installs all the packages. However, the ./node_modules/babel-core/ folder will only have these files:
browser.min.js
browser-polyfill.js
package.json
Some files are missing, like the register.js file.
However, if I manually install babel-core, like this: npm install babel-core, the ./node_modules/babel-core/ folder will be complete. The register.js file will not be missing.
What I don't understand is: How can some files be missing if I install all the packages but not be missing if I install a specific package?
OBS: I'm having this problem running on a Ubuntu VM. Node version: 0.12.X. This problem seems not to occur when I'm on Windows.

Is there a minimal version of "npm install grunt"?

When I run npm install grunt I get, in my opinion, a heavily polluted project folder. In the folder node_modules there are libs like rimraf, coffescript and others I really don't want to use. Are these required for grunt itself to be able to run or are they only there for my convenience?
I would like some kind of "npm install grunt --minimal" to only install an absolute minimum of what's needed to be able to use grunt. I don't really like my project folder full of stuff that's not really my own code. I've googled the terms i can come up with, like "minimal grunt install", but only found the basic install tutorials.
Create a node_modules directory in a directory above your project's directory and do npm install grunt there. That way the modules are accessible but they aren't in your project's node_modules directory.
Another option is to use npm install -g grunt to install grunt globally.
No, you can not. Grunt is npm package and it has dependencies (this is normal). The fact is this dependencies don't pollute your project folder structure. It is normal to
add node_modules to .gitignore and don't commit it
install every npm package with --save flag (will write package to package.json) or --save-dev flag (will write package to package.json as package needed for development)
commit package.json to repository. Thus, every team member can install all packages via npm install or npm install --production
As Dan said you can install grunt globally with -g flag but this will not solve your problem as every npm package that you install will have it own dependencies (and you'll probably think that it pollutes you folder structure as well)
You know, there are a lot of things that you need for development (or production). While installing apache (or something else) you don't say that is pollutes your file system (but it has own folder and so on).
Things don't pollute file structure if they don't exist in your project repository.
use pnpm
pnpm creates the "deep" node_modules that you want
also saves disk space and network traffic : )
alternative: npm install --legacy-bundling
to produce a deep node_modules

Categories

Resources