Search code examples
javascriptrestnpmmockingrelease

Replace URL during build step of npm


I've a mock REST service during development and a real backend I want to use for the public release. Is there a way to replace the endpoint URL somehow during the npm build step?


Solution

  • You can use process.env.NODE_ENV to measure whether it is at development environment or production environment.

    It would be something like this:

    const baseUrl = process.env.NODE_ENV === 'development' ? 'localhost:3000' : 'www.live.com'
    

    localhost:3000 would be the backend url while you developing.