Can I import public modules (.js files) in Backend events.js file?

public/vars.js file


// public/vars.js file
import wixLocation from 'wix-location';

export var globalVars = {
    srcreferral : "test"
};

export function getUrlQuery() {
    let query = wixLocation.query;
    if(query) {
        globalVars.srcreferral = query["srcreferral"];
    }
}

Products Page

// Products Page
// “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world

import {getUrlQuery} from 'public/vars.js';
import {globalVars} from 'public/vars.js';

// ...

$w.onReady(function () {
    // Write your JavaScript here

    // To select an element by ID use: $w("#elementID")

    // Click "Preview" to run your code

    getUrlQuery();

});

Backend/events.js

// Place this code in the events.js file
// of your site's Backend section. 
// Add the file if it doesn't exist.

import wixData from 'wix-data';
import {globalVars} from 'public/vars.js';

export async function wixStores_onNewOrder(event) {
  const newOrderId = event.orderId;
  const dateCreate = event.dateCreated;

  // Get the new order's line items from the Stores/Orders collection
  const orderLineItems = await wixData.get("Stores/Orders", newOrderId)
    .then((results) => {

        let newOrder = {
            orderId        : results.number,
            dateCreated    : dateCreate,
            sharer         : globalVars.srcreferral
        };
        console.log(results.items);
        wixData.insert("TestSharer", newOrder);
      return results.lineItems
    })
    .catch((error) => {
      // Order not found in the Stores/Orders collection
      console.error(error);
    });
}

Yes, you can use public files in the back-end files. But if these files use specific front-end APIs then you can’t use them on the back-end . And vice versa, if public files include some specific front-end APIs then you can’t use it on the back-end.

In your public file used a wix-location , this API is only for the frontend.

Docs:

The APIs in wix-location can only be used in front-end code and only after the page is ready.