Berry: [Bug] Can't add packages: This command can only be run from within a workspace of your project

Created on 28 Jan 2020  路  5Comments  路  Source: yarnpkg/berry

I created a folder called hello-world then ran yarn init.

It created a file called package.json with this content:

{
  "name": "hello-world"
}

Then I try to install a package, for example moment: yarn add moment.

I get this error:

Usage Error: This command can only be run from within a workspace of your project (Desktop/hello-world isn't a workspace of /C:/Users/Joel/package.json).

and /C:/Users/Joel/package.json doesn't exist, because I deleted it to see if it would fix the problem, but it didn't.

Is there a way to use yarn without dealing with all that workspace thing, I thought I could just replace npm with yarn...

bug stale

Most helpful comment

I can reproduce this on my machine (macOS).
Creating an empty yarn.lock file in your package directory (i.e. at C:\Users\Joel\Desktop\hello-world\yarn.lock) makes it work.

All 5 comments

I can reproduce this on my machine (macOS).
Creating an empty yarn.lock file in your package directory (i.e. at C:\Users\Joel\Desktop\hello-world\yarn.lock) makes it work.

Until I created the empty yarn.lock file, this bug also meant that I couldn't install yarn plugins (said it was successful when it didn't create the files), and installing packages wouldn't install or generate the cache zips.

thanks man. It麓s really working

Hi! 馃憢

This issue looks stale, and doesn't feature the reproducible label - which implies that you didn't provide a working reproduction using Sherlock. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it or you edit your first post to include a formal reproduction (you can use the playground for that).

Note that we require Sherlock reproductions for long-lived issues (rather than standalone git repositories or similar) because we're a small team. Sherlock gives us the ability to check which bugs are still affecting the master branch at any given point, and decreases the amount of code we need to run on our own machines (thus leading to faster bug resolution faster). It helps us help you! 馃槂

If you absolutely cannot reproduce a bug on Sherlock (for example because it's a Windows-only issue), a maintainer will have to manually add the upholded label.

I'd like to be able to have multiple packages in my repo without being forced to use workspaces.

This is the best I could do for a repro. Since the yarn command fails, it says the reproduction is invalid. I think you all need to reconsider this requirement for a reproduction using Sherlock when an issue is opened with clear and concise repro instructions.

```js repro
const fs = require('fs')

fs.writeFileSync('package.json', JSON.stringify({
name: 'root-package',
version: '1.0.0'
}))

fs.mkdirSync('sub-package')

fs.writeFileSync('sub-package/package.json', JSON.stringify({
name: 'sub-package',
version: '1.0.0'
}))

process.chdir('sub-package')
const output = await yarn(install);

expect(output).not.toContain(Usage Error);
```

Was this page helpful?
0 / 5 - 0 ratings