How to prevent npm install on App

Posted November 2, 2020 1.3k views
Vue.jsDigitalOcean App Platform

It seems that the app platform always runs the npm install command when it’s building a component.
Since we compile our assets on our development platform the npm install command is unnecessary.
When the package.json file is removed it doesn’t run this command, but I want to keep this file in our repo.

Is there any reason this command is automatically executed. And is there any way to prevent this?

edited by AHA

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

There isn’t a directly configurable option that stops npm install from running. The most flexible way to customize the build process is to use a Dockerfile which gives you complete control over the build process and the resulting container image.

For npm install specifically there is a workaround that you can use. If you do not need it at all, you can hijack the preinstall script to remove the file completely, like so:

  "scripts": {
    "preinstall": "[[ $STOP_NPM_INSTALL == 1 ]] && rm package.json || true"

If the STOP_NPM_INSTALL env var is set to 1, when npm install is run the package.json file will “self-destruct” stopping the install process without any errors. Otherwise the process continues as usual.

Deleting the whole file is a little crude but it works. You could expand it a bit to run an external script that more carefully only removes the dependencies field from the file if you wish.

I hope that helps! Out of curiosity, which programming language/build environment does your app use?

  • I can’t get the pre-install script to work, but I’ll try some more later. For now I have added the build command rm package.json && rm package-lock.json. The files are deleted but it still installs the node_modules, very weird.

    Creating a Docker file can also be an option, but don’t have enough experience with that at the moment.

    Our app is built in Laravel with VueJS.

    • The build command is executed at the end of the build after any internal build operations have finished, which includes npm install. That’s why it’s not affecting the build behavior.

      Did you also add the STOP_NPM_INSTALL environment variable to your component after making the change to package.json?

      • I did not do that yet because it gives an error already with npm install at my development computer:

        '[[' is not recognized as an internal or external command,

        The syntax for getting the variable needs to be different I think.

        • Got it. The command I posted will definitely not work on Windows - my bad. I think a decent different approach could be to write a node program using the stdlib to delete package.json if the env var is set, and have preinstall run it, for example::

          "scripts": {
            "preinstall": "node remove_node_modules.js"