How to replace the registry from package-lock.json to a new one?

Viewed 938

Currently, the package registry in the package-lock.json is http://registry.npm.taobao.org/.

package-lock.json:

{
  "name": "webpack",
  "version": "1.0.0",
  "lockfileVersion": 1,
  "requires": true,
  "dependencies": {
    "@types/clean-webpack-plugin": {
      "version": "0.1.0",
      "resolved": "http://registry.npm.taobao.org/@types/clean-webpack-plugin/download/@types/clean-webpack-plugin-0.1.0.tgz",
      "integrity": "sha1-xJillaQXSwoxPOQt5+UldcEi9aQ=",
      "dev": true,
      "requires": {
        "@types/webpack": "*"
      }
    },
    "@types/node": {
      "version": "9.4.0",
      "resolved": "http://registry.npm.taobao.org/@types/node/download/@types/node-9.4.0.tgz",
      "integrity": "sha1-uFoLzx4cyE60kBt+lpZq7cbweNE=",
      "dev": true
    },
    "@types/strip-bom": {
      "version": "3.0.0",
      "resolved": "http://registry.npm.taobao.org/@types/strip-bom/download/@types/strip-bom-3.0.0.tgz",
      "integrity": "sha1-FKjsOVbC6B7bdSB5CuzyHCkK69I=",
      "dev": true
    },
    // ...others
}

I want to replace all registries with the npm official registry https://registry.npmjs.org/ or any specific registry. Keep using the same versions of packages.

How can I do that?

1 Answers

Found a solution from issue of yarn.

Before replacing the registries, let's check how many registries need to be replaced, I will check the number of taobao words to determine.

☁  v4 [master] ⚡  grep -o 'taobao' package-lock.json | wc -l
    1180

Since some registries' pattern like this:

"resolved": "https://registry.npm.taobao.org/@hapi/joi/download/@hapi/joi-15.1.1.tgz?cache=0&sync_timestamp=1603524515155&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40hapi%2Fjoi%2Fdownload%2F%40hapi%2Fjoi-15.1.1.tgz",

The other_urls query string contains the url address of the taobao registry. The // symbol for the https protocol is escaped, so I will just replace registry.npm.taobao.org instead of https://registry.npm.taobao.org.

Besides, I removed the old node_modules using rm -rf node_modules, later, I will install node modules after replacing the registry with the new one, re-run npm install and run my application to check if it works.

Now, replace the registry using the below Linux command:

sed -i -e "s#registry.npm.taobao.org#registry.npmjs.org#g" package-lock.json

Check:

☁  v4 [master] ⚡  grep -o 'taobao' package-lock.json | wc -l                                  
       0
☁  v4 [master] ⚡  grep -o 'npmjs' package-lock.json | wc -l
    1180

After re-install the node modules with the new registries, my project generated by create-react-app tool works fine.

Related