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

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.

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.

How to use npm workspace

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

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 inject pouchdb-browser

I work on an web app with pouchdb, and protractor for e2e tests.
Recently, following pouchdb documentation, we switch from npm pouchdb package to pouchdb-browser package, which is more suitable for in-browser use.
It has a side effect as we also inject pouchdb from portractor in order to force some state during e2e test:
export namespace pouchBackdoor {
function _inject(): void {
browser.executeScript('return typeof PouchDB == "undefined"').then(res => {
if (res) {
let lib = path.join(
__dirname, '../node_modules/pouchdb/dist/pouchdb.js');
browser.executeScript(fs.readFileSync(lib, 'utf-8'));
}
});
}
Following the modification node_modules/pouchdb doesn't exist anymore and our test dont pass.
A quick workaround is to re-install the complete pouchdb distribution for the dev environment, but it's not really satisfying as everything we need should be in pouchdb-browser already.
So I tried to find the equivalent of node_modules/pouchdb/dist/pouchdb.js file in node_modules/pouchdb-browser/ folder but it turns out that i'm not able to do it.
I tried to use node_modules/pouchdb-browser/lib/index.js but it returns the following error:
Failed: unknown error: require is not defined
Which make sence as require doesn't exist in browser environment.
So my question is, does anyone tried to do the same and find a solution, or you does anyone know better how npm works and can give me any leads ?
PS: there is the contain of my node_modules/pouchdb-browser/ folder after a clean npm install pouchdb-browser:
pouchdb-browser/
├── lib
│   ├── index.es.js
│   └── index.js
├── LICENSE
├── node_modules
│   └── lie
│   ├── ...
├── package.json
└── README.md
Thanks for reading !
Edit:
Issue open in github/pouchdb to ask for pouchdb.js inclusion in pouchdb-browser package:
https://github.com/pouchdb/pouchdb/issues/6745

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