question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scoped packages on forked versions

See original GitHub issue

Hello, everyone!

I know we had a PR merged to accept NPM scoped packages in --scripts-version, this is another issue. I recently have been maintaining a forked version of create-react-app where I mostly customized webpack configs to add things like cssnext, stylelint, etc. The problem happened when I updated ./packages/react-scripts/package.json to be a scoped package having the name to be something like @myOrg/my-package-name.

We all know that scoped packages will create a folder between node_modules and react-scripts, which is perfectly fine! I had of course to do some tweaks in few relative paths and everything worked.

The real problem is not with my published NPM package version, but instead with my local scripts. I’m trying to keep react-scripts e2e tests and things working, so that I can automate my fork and make sure the experience with my forked package is always good (things always working).

Looking at ./tasks/cra.sh around line 66 we have something like:

# Finally, pack react-scripts
scripts_path=$root_path/packages/react-scripts/`npm pack`

So our buddy NPM will look at my scoped package.json:

{
  "name": "@danorg/react-scripts",
  "version": "0.0.1",
...
}

and will generate a tar file named danorg-react-scripts-0.0.1.tgz. Well, things go fine until right after ./packages/create-react-app/index.js performs the install and tries to locate the installed react-scripts folder.

The getPackageName function does:

// Extract package name from tarball url or path.
function getPackageName(installPackage) {
  if (installPackage.indexOf('.tgz') > -1) {
    // The package name could be with or without semver version, e.g. react-scripts-0.2.0-alpha.1.tgz
    // However, this function returns package name only without semver version.
    return installPackage.match(/^.+\/(.+?)(?:-\d+.+)?\.tgz$/)[1];
  } else if (installPackage.indexOf('@') > 0) {
    // Do not match @scope/ when stripping off @version or @tag
    return installPackage.charAt(0) + installPackage.substr(1).split('@')[0];
  }
  return installPackage;
}

It infers the package name from the tar file, so it thinks the folder is danorg-react-scripts but it is actually danorg/react-scripts.

I’m having problems trying to figure out the best way to fix this, my thoughts were that cra.sh and ..../create-react-app/index.js should be a little bit smarter to figure out the actual package name.

A stupid way I found to do it is sending the actual package.json name as an argument to the script. Let’s say that our cra.sh as now the following line:

...
# Finally, pack react-scripts
scripts_path=$root_path/packages/react-scripts/`npm pack`
packageName=`node -e 'console.log(require("./package.json").name)'`

...
node packages/create-react-app/index.js --scripts-version=$scripts_path "$@" --package-name=$packageName

But I don’t really know if that is the right way to fix this, maybe I shouldn’t be using scoped packages.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danielfigueiredocommented, Mar 8, 2017

@EnoahNetzach I just rebased from facebook and it works! Thanks a lot

1reaction
EnoahNetzachcommented, Feb 12, 2017

I don’t particularly like to add another cli argument.

We could enhance create-react-app/index.js to look for directories too when the packageName contains (but doesn’t begin with) “react-script”…

By the way, @danielfigueiredo, is this what you mean?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and publishing an organization scoped package
As an organization member, you can create and publish public and private packages within the organization's scope. Creating an organization scoped ...
Read more >
How to change NPM scoped package to a github repo
I've forked a repository from @react-native-firebase/admob to my own github and made some changes to it, and I want to change the existing ......
Read more >
Etiquette around forks & npm - Open Source Stack Exchange
If your PRs aren't being accepted, the best course of action is to published a scoped version of the package. You can do...
Read more >
Project forking workflow - GitLab Docs
A fork is a personal copy of the repository and all its branches, which you create in a namespace of your choice. This...
Read more >
dealing with problematic dependencies in a restricted network ...
Step 2: Fork-publish the direct dependency · Create a new branch to hold your customizations · Add your scope to the package name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found