Created by: lukekarrys
This checks for the existence of process.env.npm_config_user_agent to determine how create-react-app was invoked. If npm_config_user_agent is missing, the script will fail with an error message similar to
It is still possible for npx to use a global or local install if one exists. In those cases, the checkForLatestVersion will cause it to fail if the user is not up to date.
Closes #11473 (closed)
To confirm that this was the best env var to check for I wrote a quick package that prints npm_* env vars when it is invoked, and ran it via npx, yarn create, yarn dlx, and when installed globally. The results of those are below if anyone is curious, but it npm_config_user_agent was the best choice (same as #11322).
Details
npx
npx @lukekarrys/create-test-env
{
"__dirname": "/Users/lukekarrys/.npm/_npx/d4632730833703dd/node_modules/@lukekarrys/create-test-env",
"npm_config_metrics_registry": "https://registry.npmjs.org/",
"npm_config_sign_git_tag": "true",
"npm_config_noproxy": "",
"npm_config_globalconfig": "/usr/local/etc/npmrc",
"npm_package_bin_create-test-env": "index.js",
"npm_execpath": "/usr/local/lib/node_modules/npm/bin/npx-cli.js",
"npm_package_json": "/Users/lukekarrys/Desktop/create-test-env/package.json",
"npm_config_userconfig": "/Users/lukekarrys/.npmrc",
"npm_config_init_module": "/Users/lukekarrys/.npm-init.js",
"npm_command": "exec",
"npm_lifecycle_event": "npx",
"npm_package_name": "@lukekarrys/create-test-env",
"npm_config_node_gyp": "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js",
"npm_package_version": "1.0.2",
"npm_config_cache": "/Users/lukekarrys/.npm",
"npm_lifecycle_script": "create-test-env",
"npm_config_user_agent": "npm/7.21.1 node/v16.9.1 darwin x64 workspaces/false",
"npm_node_execpath": "/usr/local/Cellar/node/16.9.1/bin/node",
"npm_config_prefix": "/usr/local"
}
npm i -g
npm i -g @lukekarrys/create-test-env
create-test-env
{
"__dirname": "/usr/local/lib/node_modules/@lukekarrys/create-test-env"
}
yarn create
yarn create @lukekarrys/test-env
{
"__dirname": "/Users/lukekarrys/.config/yarn/global/node_modules/@lukekarrys/create-test-env",
"npm_package_dependencies__lukekarrys_create_test_env": "^1.0.2",
"npm_config_version_git_tag": "true",
"npm_config_yarn_path": "/Users/lukekarrys/.yarn/releases/yarn-1.22.11.cjs",
"npm_config_init_license": "MIT",
"npm_config_sign_git_tag": "true",
"npm_config_registry": "https://registry.yarnpkg.com",
"npm_package_dependencies_create_react_app": "^4.0.3",
"npm_config_python": "/usr/bin/python",
"npm_execpath": "/Users/lukekarrys/.yarn/releases/yarn-1.22.11.cjs",
"npm_config_argv": "{\"remain\":[],\"cooked\":[\"\"],\"original\":[\"create\",\"@lukekarrys/test-env\"]}",
"npm_lifecycle_event": "create",
"npm_package_name": "",
"npm_config_version_commit_hooks": "true",
"npm_config_bin_links": "true",
"npm_package_version": "",
"npm_config_save_prefix": "^",
"npm_config_strict_ssl": "true",
"npm_config_version_git_message": "v%s",
"npm_config_ignore_path": "1",
"npm_config_version_git_sign": "",
"npm_config_ignore_scripts": "",
"npm_config_user_agent": "yarn/1.22.11 npm/? node/v16.9.1 darwin x64",
"npm_config_init_version": "1.0.0",
"npm_config_ignore_optional": "",
"npm_node_execpath": "/usr/local/Cellar/node/16.9.1/bin/node",
"npm_config_version_tag_prefix": "v"
}
yarn global add
yarn global add @lukekarrys/create-test-env
create-test-env
{
"__dirname": "/Users/lukekarrys/.config/yarn/global/node_modules/@lukekarrys/create-test-env"
}
yarn dlx
yarn dlx @lukekarrys/create-test-env
{
"__dirname": "/private/var/folders/yt/5msscr_d2g57cy9jdm_r1ndh0000gn/T/xfs-e037e44c/dlx-32054/node_modules/@lukekarrys/create-test-env",
"npm_execpath": "/private/var/folders/yt/5msscr_d2g57cy9jdm_r1ndh0000gn/T/xfs-85ac12e0/yarn",
"npm_node_execpath": "/private/var/folders/yt/5msscr_d2g57cy9jdm_r1ndh0000gn/T/xfs-85ac12e0/node",
"npm_package_name": "root-workspace-0b6124",
"npm_package_version": "",
"npm_config_user_agent": "yarn/3.0.2 npm/? node/16.9.1 darwin x64"
}