Skip to content

Instantly share code, notes, and snippets.

@manzt

manzt/README.md Secret

Last active September 28, 2023 19:52
Show Gist options
  • Save manzt/621fe167e0c7f3764b94e7135951653d to your computer and use it in GitHub Desktop.
Save manzt/621fe167e0c7f3764b94e7135951653d to your computer and use it in GitHub Desktop.
npm pack # or npm publish
tar -Oxf blah-1.0.0.tgz package/package.json
# {
#   "name": "blah",
#   "version": "1.0.0",
#   "type": "module",
#   "exports": {
#     ".": {
#       "types": "./dist/index.d.ts",
#       "import": "./dist/index.mjs"
#     }
#   },
#   "scripts": {
#     "backup": "node backup.js",
#     "prepack": "npm run backup && node prepack.js",
#     "postpack": "node postpack.js"
#   }
# }
import * as fs from "node:fs";
fs.writeFileSync("./package.json.backup", fs.readFileSync("./package.json"));
{
"name": "blah",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"types": "index.d.ts",
"import": "index.js"
}
},
"scripts": {
"backup": "node backup.js",
"prepack": "npm run backup && node prepack.js",
"postpack": "node postpack.js"
}
}
import * as fs from "node:fs";
const backup = fs.readFileSync("./package.json.backup");
fs.writeFileSync("./package.json", backup);
fs.unlinkSync("./package.json.backup"); // delete backup file
import * as fs from "node:fs";
const contents = JSON.parse(fs.readFileSync("./package.json", { encoding: "utf8" }));
contents.exports["."] = {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
}
fs.writeFileSync("./package.json", JSON.stringify(contents, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment