How can I use npm install after deploying an app?
-
I'm working on strapi and ran into an issue when installing plugins from the marketplace. This is running on node and plugins also run node commands. The problem: node_modules is in a read only location /app/pkg/app/node_modules, so the installation fails for obvious reasons.
I couldn't find an app which does that, any pointers on how to overcome that? (other than moving everything to /app/data)
-
@msbt There is no way to overcome that. It's by design. The app's code cannot be changed after build. The only work-around, like you said, would be to move to
/app/data
. Not necessarily "everything". You only have to symlink to/app/data
the few paths which should still be writable after install -
Yes that is an unfortunate situation, but if possible either first package without custom plugin support or maybe there is a way to have a secondary folder for plugins to be used.
Generally for the app itself, if that is going to be installed during startup with npm/yarn, this is not really acceptable since the app itself then may or may not differ from instance to instance, depending on how strictly the project has locked dependency versions or worse npm might fail intermittedly.
-
As @mehdi explained, symlinking is probably your best bet. If you know the directory where the node plugins/packages will go, symlink that dir pre-build:
cd /app/data mkdir node_modules ln -s /app/data/node_modules /app/code/node_modules
Please take note, I am not using trailing "/" here as that has been known to cause weird issues in the past for me. The above will create a symlink in /app/code that is read-write so npm install should work (you will get cache errors since /root is not writable (or where ever npm detects it's home dir to be) but cache is not needed for npm.
This should only be done once though either during the build (if module license allows) or during the first startup. Otherwise as @nebulon explains - instance to instance code may break (like if a module was updated in a later version of the app.)
-
@msbt I faced a similar issue with node-red. While I haven't tried it yet, I think what we want is to set NODE_PATH. See https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders . I guess we set NODE_PATH=/app/data/custom-modules or something ?