--allow-worker


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active development

使用 权限模型 时,进程默认无法创建任何工作线程。出于安全原因,除非用户在主 Node.js 进程中显式传递标志 --allow-worker,否则调用将抛出 ERR_ACCESS_DENIED

¥When using the Permission Model, the process will not be able to create any worker threads by default. For security reasons, the call will throw an ERR_ACCESS_DENIED unless the user explicitly pass the flag --allow-worker in the main Node.js process.

示例:

¥Example:

const { Worker } = require('node:worker_threads');
// Attempt to bypass the permission
new Worker(__filename); 
$ node --permission --allow-fs-read=* index.js

Error: Access to this API has been restricted
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_ACCESS_DENIED',
  permission: 'WorkerThreads'
}