1

I am trying to build the Javascript end of my app using npm run build via the setup.py config file. I am using the build class from distutils as suggested elsewhere, but I am getting an error when I run pip install .

from setuptools import setup
from distutils.command.build import build
import json
import os

class javascript_build(build):
    def run(self):
        self.run_command("npm run build")
        build.run(self)

if __name__ == "__main__":
    setup(
        cmdclass={'build': javascript_build},
         )

Does anyone know why is this happening?

 running npm run build
 error: invalid command 'npm run build'
 ----------------------------------------
 ERROR: Failed building wheel for chemiscope

EDIT 1: So it seems that instead of running npm run build, it is running python setup.py npm run build. So my question changes a little to how do I exactly force distutils to run npm run build?

2
  • Have you installed nodejs in the app before? npm would not run until the system where you want to install has nodejs runtime. Commented Aug 11, 2021 at 12:03
  • Yes, I can normally run npm run build from the command line. Only when I try to do it via the setup.py file, does it produce this invalid command thing. Commented Aug 11, 2021 at 12:05

2 Answers 2

2

self.run_command("xxx") doesn't run a program — it calls another distutils/setuptools subcommand; something like calling python setup.py xxx but from the same process, not via the command line. So you can do self.run_command("sdist") but not self.run_command("npm").

In your case you need os.system("npm run build") or subprocess.call("npm run build").

Sign up to request clarification or add additional context in comments.

Comments

1

I have managed to get this working by using subprocess.check_output() as shown below. I am not sure if this is ideal, but it does the job.

from setuptools import setup
from distutils.command.build import build
from distutils import log
import subprocess
import json
import os

class javascript_build(build):
    def run(self):
        log.info("running npm run build")
        subprocess.check_output(['npm', 'run', 'build'], shell=True)
        build.run(self)


if __name__ == "__main__":
    setup(
        cmdclass={
            'build': javascript_build,
            },
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.