Yarn 2 init, add failing - yarn-v2

I'm experimenting with yarn 2 and faceplanting.
I created a new folder: /projects/yarn2/
As per their install guide https://yarnpkg.com/getting-started I ran
cd /projects/yarn2/
yarn set version berry
yarn init
then (as per their guide https://yarnpkg.com/getting-started/usage )
yarn add react
and got the following error:
Usage Error: The nearest package directory (/projects/yarn2) doesn't seem to be part of the project declared in /projects.
- If the project directory is right, it might be that you forgot to list yarn2 as a workspace.
- If it isn't, it's likely because you have a yarn.lock or package.json file there, confusing the project root detection.
$ yarn add [--json] [-E,--exact] [-T,--tilde] [-C,--caret] [-D,--dev] [-P,--peer] [-O,--optional] [--prefer-dev] [-i,--interactive] [--cached] ...
What am I doing wrong?

You either don't have package.json or yarn.lock which confuses yarn if the package is added in the workspace or not. Just run the following command and I think your problem must be solved.
cd <folder_name>/<project_name>
touch yarn.lock
yarn

Check to see if you have a package.json or yarn.lock file in your /projects directory. If you do, clear it/them out and this should start working.

To add to the previous answers what worked for me was:
It seems to be that yarn.lock file is what's really required, so if you have started with yarn v1.x and did yarn init - then you will have a package.json file in the project dir already.
If you now switch over to yarn v2 by doing yarn set berry, and want to add a package by yarn add [whatever] it will fail. You can work around that by creating an empty file by: touch yarn.lock (or on windows just cat '' > yarn.lock) after that it should work fine.
I figured out an even better / faster way to do it now, just do this in an empty project folder:
yarn init -2
this will initialize a new yarn v2 project folder with .yarnrc.yml, package.json, and the .yarn folder, also initializes an empty .git folder and adds a proper .gitignore. check by yarn --version - should echo v2.x.x

Usage Error: The nearest package directory (/<path_to_folder>/<project_name>) doesn't seem to be part of the project declared in /<path_to_folder>.
Solution:
Search for yarn.lock and package.json files inside /<path_to_folder> and delete them!

you have to remove yarn.lock and package.json file in your root directory ,some times there may be .yarnrc yarnyml files in you root directory you have to remove them also.

Related

When using yarn-berry, should I apply the unplugged to the git repository when implementing zero-installs?

When using yarn-berry, should I apply the unplugged to the git repository when implementing zero-installs?
As far as I know, in the case of zero-installs, we proceed with yarn start immediately after receiving
git clone from the git repository.
However, because of the Unplugged Folder, we have to proceed with yarn installs inevitably.
Of course, there are ways to put UnpluggedFolder in git repository using git-ignore, but I'm not sure if this is the correct way to run zero-installs.
When I tried yarnstart
$ yarn start
C:\Users\com\Desktop\zero3\project\.pnp.cjs:25991
Error.captureStackTrace(firstError);
^
Error: Required unplugged package missing from disk. This may happen when switching branches without running unplugged packages must be fully materialized on disk to work).
Missing package: open#npm:8.4.0
Expected package location: C:\Users\com\Desktop\zero3\project\.yarn\unplugged\open-npm-8.4.0-df63cfe5dules\open\
gitignore
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
# !.yarn/unplugged

Yarn can't find private Github npm registry

I signed up for the Github private npm registry beta and followed their instruction: https://github.com/features/package-registry
Works great with npm but I'd prefer using yarn. And while npm has no issues finding the registered package, yarn can't find it at all.
yarn add #omniphx/adminite-adminite-ui-components outputs:
yarn add v1.19.0
info No lockfile found.
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] 🔍 Resolving packages...
error Couldn't find package "#omniphx/adminite-ui-components" on the "npm" registry.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
After reading up on private repos with yarn, I thought the trick was due to yarn having a slightly different rc format. Unfortunately, that didn't work either and yarn is still unable to find the private registry.
.npmrc
registry=https://registry.npmjs.org
#omniphx:registry=https://npm.pkg.github.com/omniphx
.yarnrc
registry "https://registry.npmjs.org"
"#omniphx:registry" "https://npm.pkg.github.com/omniphx"
Also confirmed that my github token is set too with yarn config list:
yarn config v1.19.0
info yarn config
{
'version-tag-prefix': 'v',
'version-git-tag': true,
'version-commit-hooks': true,
'version-git-sign': false,
'version-git-message': 'v%s',
'init-version': '1.0.0',
'init-license': 'MIT',
'save-prefix': '^',
'bin-links': true,
'ignore-scripts': false,
'ignore-optional': false,
registry: 'https://registry.npmjs.org',
'strict-ssl': true,
'user-agent': 'yarn/1.19.0 npm/? node/v12.11.1 darwin x64',
email: 'mattjmitchener#gmail.com',
lastUpdateCheck: 1570679687836,
username: 'omniphx',
'#omniphx:registry': 'https://npm.pkg.github.com/omniphx'
}
info npm config
{
'//npm.pkg.github.com/:_authToken': 'fake12345',
registry: 'https://registry.npmjs.org',
'#omniphx:registry': 'https://npm.pkg.github.com/omniphx',
python: '/usr/bin/python'
}
Any idea?
Resolved
Changed "#myorg:registry" "https://npm.pkg.github.com/myorg"
To "#myorg:registry" "https://npm.pkg.github.com"
I've just run into a similar situation. It seemed that yarn was only looking in the main Yarn package registry for my organization's private package. I had copied the examples from GitHub's Packages documentation for constructing your .npmrc file directly to the .yarnrc file in the project that will be consuming the app, not knowing that the formats were different (I've never had to deal with .yarnrc files before).
However, after updating the .yarnrc file with the correct format that you've mentioned above (which I also found in googling around), yarn successfully found the private package and installed it correctly.
As a heads up, my yarn version: 1.17.3
Steps I Took
Start new terminal session
cd to the project
nvm use (if you have a specific node version to use)
Add the correctly-formatted .yarnrc file to the project. See below for what it looks like.
Manually add the package and version range to the package.json for my private package
Run npm login --registry=https://npm.pkg.github.com --scope=#MyOrg
See the note below on scope / org gotcha's
Run yarn
That worked for me.
.yarnrc
"#myorg:registry" "https://npm.pkg.github.com"
Note: See below for a note on the org / scope name gotcha's
Other Notes
I know that it appears that you don't have any issues with this, given your GH username / scope above, but for anyone else that comes here, the documentation on GH is a little sparse with regards to mapping your username / org name to a scope in the package name. Just remember these little gotcha's here:
The name of your package must always be scoped to your org (or username)
E.g., name: #johndturn/my-package
If your organization has capital letters in it, like MyOrg, just replace them in the name of the package in your package.json and your .yarnrc with lowercase
E.g., name: #myorg/my-package
Note: When authenticating with npm login, I still have kept the uppercase letters in the --scope= argument.
The name of your package doesn't have to be the same name of the repo.
E.g., for a repo called MyOrg/random-prefix.js-lib, you can have name: #myorg/js-lib in your package.json file for the project itself. Then, installing it in other projects will look something like #myorg/js-lib: 1.0.0.
The problem I had is slightly different.
After tried what John suggested I still can't add private registry packages with yarn (but perfectly fine with npm)
Then I realise two things:
For GitHub packages, npm is fine with either
registry=https://npm.pkg.github.com/my-org
or
#my-org:registry=https://npm.pkg.github.com
but yarn only allow the latter syntax.
Docs from Github website only show the first syntax which could cause problems for yarn users.
Another thing is that if you npm login to the private registry but use a .yarnrc file in your project, yarn can't really mix your npm credentials with it. Although it seems behave differently on different environment.
But it would seems to be a best practice to stick with either yarn login + .yarnrc, or npm login + .npmrc (you can still use yarn to manage your packages in both cases)
In Yarn v2+ the setup has changed quite a bit. ".yarnrc" is ignored and only ".yarnrc.yml" is used.
To setup a private registry with a scope and token from env, add something along these lines to the ".yarnrc.yml" file (fontawesome example):
npmScopes:
fortawesome:
npmRegistryServer: "https://npm.fontawesome.com"
npmAuthToken: ${FONTAWESOME_TOKEN}
Documentation: https://yarnpkg.com/configuration/yarnrc#npmScopes
I'm not an expert with npm/yarn so I might be misunderstanding what is happening here, but I don't think package proxying from the npm registry works with yarn yet. Could that be related? When package proxying was released for npm I remember reading comments on Twitter from people that tried it with yarn and it didn't work.
Found the Twitter thread here:
https://twitter.com/github/status/1171832034580451328
It doesn't work with Yarn. As soon as I change the registry url -> Couldn't find package.

node_modules not installed when running 'vsce package' command

I wrote a VSCode extension and am trying to publish it locally via vsce package. When I install my app through the VSIX file, there's no 'node_modules' folder inside my extension which leads to my extension not loading correctly because no modules are found
I have checked that my required dependencies are indeed inside dependencies and not peerDependencies or devDependencies. I should note that I have a .vscodeingore file containing the following:
.vscode/**
.vscode-test/**
out/test/**
src/**
.gitignore
vsc-extension-quickstart.md
**/tsconfig.json
**/tslint.json
**/*.map
**/*.ts
node_modules
webpack.config.js
I would like that my node_modules be installed and I would see a "node_modules" folder inside my './vscode/extensions/' folder.
Thanks!

No lintable files found at paths SwiftLint

I tried to install SwiftLint using CocoaPods and I add in Build phases the following script :
"${PODS_ROOT}/SwiftLint/swiftlint"
SwiftLint is installed correctly and I get many errors and warnings in the project.
Then, I create the swiftLint.yml file in which I modify some rules but they are not token into consideration and the same number of errors and warnings persist in Xcode project.
When I run this command to confirm the application of the rules :
./swiftlint lint --config .swiftlint.yml
I get the error :
No lintable files found at paths : ''
How can I solve this issue please?
It happens also if you rename the directory of your app, make sure you report the change in the .swiftlint.yml too at first lines :
included: # paths to include during linting
- My_App_Directory
For those of you who used 0.42.0 before and updated to 0.43.0 (or higher?).
They made a change and now interpret included and excluded as relative paths.
Configuration files now consistently have their included/excluded
relative file paths applied relative to their location in the file
system. Previously the root configuration file applied these relative
to the current working directory, but nested configurations applied
these to their location in the file system.
From the release notes of 0.43.0: Clothes Line Interface.
if you are using swiftLint with CocoaPods : try "${PODS_ROOT}/SwiftLint/swiftlint" --config .swiflint.yml in your SwiftLint Run Script in your project build phases.
make sure your .swiflint.yml config file is in the root of your project directory ( beside .xcodeproj file ).
make sure the paths included on your .swiflint.yml (in included: and excluded: sections ) is valid paths
make sure your .swiflint.yml file is valid yaml
don't escape the directory paths in your config file
dont do : - some\ Directorybut do - some Directory without escape character.
If you installed it using Cocoapods this can help you.
I will just merely improve the above answers, to put clarity on how to resolve the issue of SwiftLint not finding the path.
Things to lookout for.
Make sure your swiftlint.yml file is valid.
Make sure the swiftlint.yml is in the same level as your .xcodeproj
Don't specify --path and also add an entry under included: inside your yml file, choose one, either specify the --path or add an entry don't use both otherwise SwiftLint will ignore the --path param, and if the entry specified inside included: is wrong you will get the "no lintable file found" error
In your script.
The gihub page of SwiftLint recommends just using "${PODS_ROOT}/SwiftLint/swiftlint" but that didn't work for me, I had to specify the --path see below for the full script.
"${PODS_ROOT}/SwiftLint/swiftlint" --path "${SRCROOT}/Classes" --config "directory-of-your-config"
The --path param should be your own --path "${SRCROOT}/Classes"
Finally inside the yml file.
Make sure your included and excluded file specification is correct, see how I did mine below.
included:
- Your-Project-Name
excluded:
- Pods
One Important thing to note is if you add directories under included: the --path param will be ignored, and you might possibly get the "no lintable files found" error if the directory is wrong.
First of all, you do not need to add explicitly the--config file if the yml file is in the running directory (from where you are running the command) and name is .swiftlint.yml.
Secondly, you need to specify the path for your source with --path. Below command will work fine in your case,
swiftlint lint --path SourcePath
For swiftlint from version 0.41 the following code worked for me in the build phase (workspace with several projects. Depending on the project configuration, it may be that "../" has to be removed from the path information):
cd ${PROJECT_DIR}/../
"${PODS_ROOT}/SwiftLint/swiftlint" --config "${PROJECT_DIR}/../.swiftlint.yml"
Here is a screenshot of the build phase entry:
Replace autocorrect with --fix
export PATH="$PATH:/opt/homebrew/bin" //This line is only for Apple Silicon chips
if which swiftlint > /dev/null; then
swiftlint --fix && swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
For M1, don't go for swift lint installation VIA PODS instead use Brew.
For installation run below command in Terminal
brew install swiftlint
and add below scripts into RunScript into build phase of your target.
export PATH="$PATH:/opt/homebrew/bin"
if which swiftlint > /dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
If you using pod file then follow this steps:
Run Script: "${PODS_ROOT}/SwiftLint/swiftlint"
This worked for me
if which "${PODS_ROOT}/SwiftLint/swiftlint" >/dev/null; then
${PODS_ROOT}/SwiftLint/swiftlint --fix && ${PODS_ROOT}/SwiftLint/swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

BabelJS: Doesn't find all .js files in directory

Babel doesn't find all of my .js/.es6 files in my directory.
I have this directory structure:
src/
assets/
sample/
models.es6
scripts/
playground.es6
If I run babel src --out-dir dist --source-maps --copy-files --presets env, it only transpiles /src/assets/sample/models.es6 and doesnt go through src/scripts/playground.es6.
What am I doing wrong?
Looking forward to your response!
You can do like below :
babel src/** --out-dir lib
more at official doc
Compile Directories
Compile the entire src directory and output it to the lib directory. You may use --out-dir or -d. This doesn’t overwrite any other files or directories in lib.
if you still stuck, you can use gulp or grunt or webpack to load/transpile mupltiple directives from different locations.
Hope it helps
I found the problem. It has barely to do with Babel.
Inside the src/assets/** is my Realm database sample.realm (https://realm.io). The file itself doesnt cause the problem. But if you open the sample.realm file with Realm Studio on MacOSX, a file called sample.realm.note gets created. This file causes babel to not exit the transpile task.