Search code examples
node.jsvisual-studio-codepnpm

How to specify "packageManager" in package.json


As per https://nodejs.org/dist/latest-v16.x/docs/api/all.html#all_packages_packagemanager We can specify package manager from the list of supported package managers.

However, the VS code complains that the value is not correct.

vs code error

I have also tried following values which also gives warnings

@jonrsharpe, still the same warning without space

warning


Solution

  • The packageManager field requires to match the following regex

    (npm|pnpm|yarn)@\d+\.\d+\.\d+(-.+)?
    

    And

    "packageManager": "^[email protected]"
    

    seems to resolve the warning but, for example

    "packageManager": "123!@#[email protected]"
    

    also meet the regex, which implies that the former might not work as intended.

    According to the PR on nodejs (deps: add corepack #39608)

    How does Corepack work?

    The sources are located inside nodejs/corepack. The gist is that Corepack installs "jumpers" next to the Node binary (just like what already happens for the npm and npx binaries) for the supported package managers (binaries are currently pnpm, pnpx, yarn, and yarnpkg, plus an extra one for corepack itself). They thus become exposed through the $PATH environment variable. Once called, these jumpers look into the local folder to find the top-most package.json, and read the packageManager field from them. Depending on the result:

    • If no packageManager field exists, the requested version is assumed to be the defaults ones in the Corepack configuration.
    • If a packageManager field exists, then it's used as the requested version.

    And in Corepack configuration,

    "pnpm": {
      "default": "7.3.0",
      ...
    }
    

    The default version of pnpm is 7.3.0 as of now, which is used when packageManager field is omitted, So it is best to set

    "packageManager": "[email protected]"
    

    to ensure it working as intended.