Slicemachine in monorepo

Hello,

We are planning on moving to a monorepo approach — Nx — for our marketing sites. Any time we want to start Slicemachine for a different site, we have to edit sm.json to change the apiEndpoint and libraries path.

Are there any solutions for this? For example: the ability to pass a flag to start-slicemachine to specify the path to sm.json.

Thanks!

————

Example

File structure:

apps/
  site-1/
    slices/
    sm.json
  site-2/
    slices/
    sm.json
  site-3/
    slices/
    sm.json

npm run slicemachine -- --path=./site-2/sm.json

Hello @jerry.nummi, thanks for reaching out. I'm not familiar with the monorepo environment but from what I can see online it's a strategy to structure your project that isn't fixed to one technology.

You should make sure that the command line is pointing to the correct location in your machine when you run the npm run slicemachine command. So if you wanna run site-1 your console should be in apps > site-1 and so on.

The issue I am running into is Nx has one package.json file.

/
  package.json
  apps/
    site-1/
    site-2/
      slices/
      customSlices/
      ...
    site-3/
$ cd apps/site-1; start-slicemachine
`[slice-machine] An unexpected error occurred. Exiting...
Full error:  Error: [api/env]: Unrecoverable error. Could not find package.json. Exiting..

Hey there, I asked around and our DevX team also recommends trating each app/site as its own Slice Machine installation. Here are examples of how to initiate the project with Yarn, pnpm, or npm 7/8.

You can target a specific app from the root level, like this:

# Yarn: 
yarn workspace site-1 slicemachine 

# pnpm: 
pnpm -F site-1 slicemachine 

# npm 7/8: 
npm -w site-1 slicemachine

Then the other way to do this is to cd into an app and run npm run slicemachine, as if it were not in a monorepo.

I have a solution at the moment that isn't ideal but allows me to keep moving forward:

apps/
  site-1/
    package.json
    project.json
    sm.json
    slices/
  site-2/
    ...
  site-3/
    ...

site-1/package.json

This is the hack because Nx allows you to have one package.json at the root but the Slicemachine start script is expecting package.json to be in the same current working directory. (this script I believe)

{
  "devDependencies": {
    "next": "12.x"
  }
}

site-1/project.json

{
  "targets": {
    "slicemachine": {
      "executor": "nx:run-commands",
      "options": {
        "commands": [
          {
            "command": "start-slicemachine"
          }
        ],
        "cwd": "apps/site-1"
      }
    }
  }
}

Now I can run:

$ npx nx run site-1:slicemachine
$ npx nx run site-2:slicemachine
etc.

1 Like

Thanks for sharing this workaround @jerry.nummi!