How to use npm workspace - npm

I develop my app by using npm workspace.but I am totally novice to this concept so that I would like to understand npm workspace behavior
.
├── package.json
└── packages
├── a
│ └── package.json
├── b
│ └── package.json
└── c
└── package.json
package.json
{
"workspaces": [
"packages/*"
]
}
My question is
①what happens when I run npm install in root directory ?
All the packages are installed in each repository ? or root directory ?
②npm install in each project is not recommended ?
if someone has opinion, will you please let me know.
Thanks

So long as all packages have the same version of dependencies listed (e.g. thing#1.0.0), then npm install run from the root of your repository will add the dependency into the root node_modules directory.
If one of your packages (e.g. packages/c) has a different version of a dependency (e.g. thing#1.0.1) then that would appear in packages/2/node_modules)
In theory you are using npm workspaces to ensure some consistency across packages, therefore ensuring your versions are consistent will mean node_modules is only ever created in the root of your repository

Related

Yarn Workspaces seemingly not recognizing directory

Wasn't sure the best verbiage for the title, but basically I have a monorepo setup using yarn workspaces. It works for every other directory I have. Here's my layout to make it easy to visualize
├── root
├── package.json
├── node_modules
├── appsync
│ ├── package.json
├── aurora
│ ├── package.json
├── lambdas
│ ├── lambda-function-1
│ ├── └── package.json
│ ├── lambda-function-2
│ ├── └── package.json
Roughly this is as simple as I can make it without getting into too much unnecessary detail. In my root package.json I have my yarn workspaces setup like so:
"workspaces": {
"packages": [
"./*",
"aurora/*",
"lambdas/**",
]
}
The issue is this:
For every other directory I have. Yarn Workspaces is working perfectly. appsync is covered in the ./* declaration. And I have packages in that package.json that get installed property to the root directory and not in the appsync directory. Same with my lambdas. I define those differently because it wont be covered from the ./* declaration. And this works great too, no issues.
Today I added a new directory, and to my surprise saw modules being installed to aurora and not to the root. So I'm very confused whats wrong with my root package.json thats causing this? Or any ideas I can try to get more information? I have done yarn both at root and in the aurora directory as well, and no matter what I try, it always installs modules to the aurora directory and also adds a yarn.lock file to that directory as well.
Any help is appreciated.

Npm script to run both child directory folders?

How do I write a script in my Parent Folder's package.json file so that when I run npm install it installs the node modules in each folder and npm start will go to each folder and run npm start
The Frontend and Backend folder both use npm start to start up and I want to somehow do the same in the parent folder to simultaneously start both
This is the file structure:
ParentFolder
├── package.json . <--- npm install && npm start scripts
├── FrontEnd
│ ├── /node_modules
│ ├── package.json
│ └── index.js
├── Backend
│ ├── /node_modules
│ ├── package.json
│ ├── routes.js
│ └── server.js.js
Installing in two directories is easy with find
find ./*/* -maxdepth 1 -name package.json -execdir npm install \;
This looks in each directory for a package.json and executes npm install;
npm start becomes a bit harder. At least on Windows using Cygwin, I wanted to do:
npm --prefix ./FrontEnd start ./FrontEnd & npm --prefix ./Backend start ./Backend
But it wasn't actually running in the background like I expected and FrontEnd was the only one that actually started. Depending on your start script this could work for you.
Possible solutions for this could be concurrently or npm-run-all -p.

How to organise multi package flutter project and use it as dependency

I would like to organise a flutter project into a multi-package one with following requirements:
use one repository for this project
able for developers to work on the packages in this repository locally
make the packages accessible as dependencies from other projects outside of this repository
The file setup for the repository I have now is:
.
├── app_base
│ ├── ...
│ └── pubspec.yaml
├── feature
│ ├── ...
│ └── pubspec.yaml
└── README.md
I tried using path dependencies like this in app_base/pubspec.yaml:
name: app_base
dependencies:
feature:
path: ../feature
and it works for local development but if I try to use app_base in a completely different project and not use paths but a git dependency:
name: actual_app
dependencies:
app_base:
git:
url: ssh://address.to/the_repo.git
path: app_base
ref: deadbaca
it cannot resolve the transitive feature dependency:
Running "flutter packages get" in actual_app...
Error on line 21, column 11: Invalid description: "../feature" is a relative path, but this isn't a local pubspec.
path: ../feature
^^^^^^^^^^
pub get failed (65)
Process finished with exit code 65
Is there a way to make it work for both local development and used as a git dependency from other project?
Just use Git dependencies for both scenarios (locally and for other projects).
If you think this is cumbersome during local development, use path dependencies locally and change it back to Git before committing.

NPM doesn't work properly with scoped modules from git repos

I'm still waiting for the company set up its account on NPM to allow me to use private modules.
Meanwhile, I'm making it work with npm + git private repos.
In order to reduce the friction when we finally switch to NPM private modules, I was willing to declare dependencies like:
{
"dependencies": {
"#company/repo": "git+https://<token>:x-oauth-basic#github.com/company/repo"
}
}
And then, in my code, do this:
import repo from '#company/repo'
However, when I run npm installand inspect my node_modules/, instead of finding something like:
node_modules
├── #company
│   └── repo
├── ...
The resulting structure is flat, there is no #company folder:
node_modules
├── repo
├── ...
I couldn't find anything in the NPM docs about this behavior with git repos, so I want to know if this is a possible bug or if it's working as designed.

Installing Yeoman on Yosemite

I'm on Yosemite, with Homebrew, Node and NPM installed.
Homebrew 0.9.5
Node v0.12.3
NPM 2.9.1
For whatever reason, I can't get Yeoman to properly install. The install seems to go fine, no errors, but when I check to confirm the install it wont give me the --version, nor can I execute the yo function...
Gregs-MacBook-Pro:~ G$ npm install -g yo
/Users/G/npm/bin/yo -> /Users/G/npm/lib/node_modules/yo/lib/cli.js
> yo#1.4.6 postinstall /Users/G/npm/lib/node_modules/yo
> yodoctor
Yeoman Doctor
Running sanity checks on your system
✔ Global configuration file is valid
✔ No .bowerrc file in home directory
✔ No .yo-rc.json file in home directory
✔ NODE_PATH matches the npm root
Everything looks all right!
yo#1.4.6 /Users/G/npm/lib/node_modules/yo
├── titleize#1.0.0
├── array-uniq#1.0.2
├── figures#1.3.5
├── user-home#1.1.1
├── opn#1.0.2
├── async#0.9.2
├── humanize-string#1.0.1 (decamelize#1.0.0)
├── sort-on#1.2.0 (dot-prop#2.0.0)
├── yeoman-character#1.0.1 (supports-color#1.3.1)
├── string-length#1.0.0 (strip-ansi#2.0.1)
├── cross-spawn#0.2.9 (lru-cache#2.6.4)
├── findup#0.1.5 (commander#2.1.0, colors#0.6.2)
├── chalk#1.0.0 (escape-string-regexp#1.0.3, ansi-styles#2.0.1, supports-color#1.3.1, strip-ansi#2.0.1, has-ansi#1.0.3)
├── yosay#1.0.4 (ansi-regex#1.1.1, ansi-styles#2.0.1, word-wrap#1.0.3, strip-ansi#2.0.1, pad-component#0.0.1, taketalk#1.0.0, minimist#1.1.1)
├── root-check#1.0.0 (sudo-block#1.2.0, downgrade-root#1.1.0)
├── update-notifier#0.3.2 (is-npm#1.0.0, latest-version#1.0.0, semver-diff#2.0.0)
├── package-json#1.1.0 (registry-url#3.0.3)
├── npm-keyword#1.1.1 (registry-url#3.0.3)
├── meow#3.1.0 (object-assign#2.0.0, minimist#1.1.1, camelcase-keys#1.0.0, indent-string#1.2.1)
├── got#2.9.2 (lowercase-keys#1.0.0, object-assign#2.0.0, timed-out#2.0.0, is-stream#1.0.1, prepend-http#1.0.1, nested-error-stacks#1.0.0, statuses#1.2.1, infinity-agent#2.0.3, read-all-stream#2.1.2, duplexify#3.4.0)
├── fullname#1.1.0 (npmconf#2.1.2)
├── yeoman-environment#1.2.5 (escape-string-regexp#1.0.3, log-symbols#1.0.2, untildify#2.0.0, diff#1.4.0, text-table#0.2.0, debug#2.2.0, mem-fs#1.1.0, grouped-queue#0.3.0, globby#1.2.0)
├── configstore#0.3.2 (object-assign#2.0.0, xdg-basedir#1.0.1, osenv#0.1.1, graceful-fs#3.0.7, uuid#2.0.1, mkdirp#0.5.1, js-yaml#3.3.1)
├── lodash#3.9.2
├── insight#0.5.3 (object-assign#2.0.0, lodash.debounce#3.1.0, os-name#1.0.3, tough-cookie#0.12.1, request#2.55.0)
├── yeoman-doctor#1.3.2 (object-values#1.0.0, log-symbols#1.0.2, each-async#1.1.1, twig#0.7.2)
└── inquirer#0.8.5 (ansi-regex#1.1.1, cli-width#1.0.1, through#2.3.7, readline2#0.1.1, rx#2.5.2)
Gregs-MacBook-Pro:~ G$ yo -v
-bash: yo: command not found
I've opened the .bashrc (have to use sudo to be able to open + save it). Without sudo I can't save the changes.
sudo nano /.bashrc
The bashrc file reads:
# NPM packages in homedir
NPM_PACKAGES="$HOME/.npm-packages"
# Tell our environment about user-installed node tools
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your configuration
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
# Tell Node about these packages
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
I've tried installing Node thru Homebrew first and found out about this issue which I couldnt resolve either. https://gist.github.com/DanHerbert/9520689
Since that didn't I blew everything away in terminal, including node, npm, and homebrew.
Now I'm stuck, npm lets me install packages grunt-cli, bower, and yeoman, but any time I try calling those packages I get:
-bash: yo: command not found
I'm assuming it has something to do with the /.bashrc contents, somebody help :(
I had the same problem under Yosemite.
My solution was:
sudo find /usr/local/ -name yo
/usr/local//Cellar/node/4.2.1/libexec/npm/bin/yo
open $HOME/.profile
export NODE_PATH="/usr/local/lib/node_modules"
export PATH="/usr/local/Cellar/node/4.2.1/libexec/npm/bin:$PATH"
export NODE_PATH=$NODE_PATH:/usr/local/Cellar/node/4.2.1/libexec/npm/lib/node_modules
open new terminal:
yo --version
1.4.8
perhaps your node version is a problem. At beginning after install 'brew install node' I had too
node --version
v0.12.7
After
brew update
and reinstall node
node --version
v4.2.1