将权限模型与 npx 结合使用
¥Using the Permission Model with npx
如果你使用 npx
执行 Node.js 脚本,则可以通过传递 --node-options
标志来启用权限模型。例如:
¥If you're using npx
to execute a Node.js script, you can enable the
Permission Model by passing the --node-options
flag. For example:
npx --node-options="--permission" package-name
这将为 npx
生成的所有 Node.js 进程设置 NODE_OPTIONS
环境变量,而不会影响 npx
进程本身。
¥This sets the NODE_OPTIONS
environment variable for all Node.js processes
spawned by npx
, without affecting the npx
process itself.
npx
的 FileSystemRead 错误
¥FileSystemRead Error with npx
上述命令可能会抛出 FileSystemRead
无效访问错误,因为 Node.js 需要文件系统读取访问权限来定位和执行包。为了避免这种情况:
¥The above command will likely throw a FileSystemRead
invalid access error
because Node.js requires file system read access to locate and execute the
package. To avoid this:
-
使用全局安装的软件包通过运行以下命令授予对全局
node_modules
目录的读取访问权限:¥Using a Globally Installed Package Grant read access to the global
node_modules
directory by running:npx --node-options="--permission --allow-fs-read=$(npm prefix -g)" package-name
-
使用
npx
缓存如果你是临时安装软件包或依赖npx
缓存,请授予对 npm 缓存目录的读取访问权限:¥Using the
npx
Cache If you are installing the package temporarily or relying on thenpx
cache, grant read access to the npm cache directory:npx --node-options="--permission --allow-fs-read=$(npm config get cache)" package-name
你通常传递给 node
的任何参数(例如,--allow-*
标志)也可以通过 --node-options
标志传递。这种灵活性使得在使用 npx
时根据需要配置权限变得容易。
¥Any arguments you would normally pass to node
(e.g., --allow-*
flags) can
also be passed through the --node-options
flag. This flexibility makes it
easy to configure permissions as needed when using npx
.