如何跨磁盘分区要求节点模块? [英] How can I require node modules across disk partitions?

查看:98
本文介绍了如何跨磁盘分区要求节点模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Node.js 版本:14.15.0
  • 操作系统:Raspbian
  • 范围(安装、代码、运行时、元数据、其他?):需要
  • Node.js Version: 14.15.0
  • OS: Raspbian
  • Scope (install, code, runtime, meta, other?): require

我有一个在 Raspberry Pi 4 上运行的 Node 程序.我最近开始使用名为 Mender 的 OTA 部署系统将更新推送到远程 RPis 上的代码.Mender 创建了一个分区系统,该系统使用两个 3.5GB 分区,一个作为主分区,另一个作为部署失败时的回滚.它有第三个分区 /data,在我的例子中大约 20GB,用于需要在更新之间保留的内容.

I have a Node program that I run on Raspberry Pi 4. I've recently started using a OTA deployment system called Mender to push updates to my code on remote RPis. Mender creates a partition system that uses two 3.5GB partitions, one as the main and the other as a rollback in the event of a failed deployment. And it has a 3rd partition /data, that is around 20GB in my case, for things that need to be persisted between updates.

我无法将整个应用程序及其所有节点模块依赖项放入 3.5GB 分区.所以我将 node_modules 目录移动到 /data 分区并创建了一个指向我的项目目录的符号链接(home/pi/myProject).这适用于模块安装,但是当我尝试 require 从我的项目中安装一个模块时,会抛出错误;

I was unable to get my entire application and all of it's node module dependencies into the 3.5GB partition. So I moved the node_modules directory to the /data partition and created a symlink that points back to my project directory(home/pi/myProject). This works for module installs but when I try to require an installed module from within my project an error is thrown;

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module '@google-cloud/pubsub'
Require stack:
- /home/pi/myProject/pwrMngmnt.js
- /home/pi/myProject/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/myProject/pwrMngmnt.js:3:20)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/pi/myProject/pwrMngmnt.js',
    '/home/pi/myProject/[eval]'
  ]
}

是否需要设置配置才能使其正常工作?

Is there a configuration I need to set to make this work?

推荐答案

您可以指定 NODE_PATH 环境变量,而不是使用符号链接.引用 NodeJS 文档:NODE_PATH 最初是为了支持在定义当前模块解析算法之前从不同路径加载模块而创建的."

Instead of doing a symlink, you could specify the NODE_PATH environment variable. Quoting the NodeJS documentation : "NODE_PATH was originally created to support loading modules from varying paths before the current module resolution algorithm was defined."

NODE_PATH 仍然受支持并且可以完美地适合您的用例 IMO.如果需要,不要忘记修复文件权限(使用 chmodchown).

NODE_PATH is still supported and could perfectly fit your use-case IMO. Don't forget to fix file permissions if needed (using chmod and chown).

例如:

export NODE_PATH="/data/node_modules"
node <your script>

此外,您可以询问 npmyarn 在这个目录安装模块.

Furthermore, you can ask npm or yarn to install modules in this directory.

使用纱线:

yarn install --modules-folder /data/node_modules

使用 npm:

mkdir -p /data/node_modules
npm install --prefix /data

<小时>

可能的相关问题:


Possible related questions:

这篇关于如何跨磁盘分区要求节点模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆