get-nonce

中文文档
1.0.1发布于 2020/04/17MITTypeScriptESM + CJS零依赖近 30 天下载 2.7亿

returns nonce

原始使用文档
文档目录4 节
  1. Why?
  2. Webpack
  3. Used in
  4. Inspiration

get-nonce

just returns a nonce (number used once). No batteries included in those 46 bytes of this library.


  • ✅ build in webpack support via __webpack_nonce__

API

  • getNonce(): string|undefined - returns the current nonce
  • setNonce(newValue) - set's nonce value

Why?

Why we need a library to access __webpack_nonce__? Abstractions!

"I", as a library author, don't want to "predict" the platform "you" going to use. "I", as well, want an easier way to test and control nonce value.

Like - nonce is supported out of the box only by webpack, what you are going to do?

This is why this "man-in-the-middle" was created. Yep, think about left-pad :)

Webpack

https://webpack.js.org/guides/csp/

To activate the feature set a webpack_nonce variable needs to be included in your entry script.

__webpack_nonce__ = uuid(); // for example

Without webpack __webpack_nonce__ is actually just a global variable, which makes it actually bundler independent, however "other bundlers" are able to replicate it only setting it as a global variable (as here in tests) which violates a "secure" nature of nonce.

get-nonce is not global.

Used in

  • react-style-singleton <- react-remove-scroll <- react-focus-on

Inspiration

Licence

MIT

版本与发布记录

get-nonce 共发布 2 个版本,首次发布于 2020/04/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2020/04/176 年前
1.0.02020/04/166 年前

关于 get-nonce 的常见问题

get-nonce 是什么?

returns nonce

如何安装 get-nonce?

在项目目录运行 npm install get-nonce,或使用 pnpm add / yarn add / bun add。

get-nonce 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

get-nonce 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

get-nonce 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

get-nonce 可以商用吗?

get-nonce 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

get-nonce 的下载量有多少?

根据 npm 官方下载统计,get-nonce 在 2026/10/05 – 2026/11/03 这 30 天内被下载 268,938,830 次,日均约 8,964,628 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

get-nonce 还在维护吗?

最新版本 1.0.1 发布于 2020/04/17(6 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/04/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。