Like what we’re doing? Star us on GitHub!

Import a script to collection to use inside a hook

vstarush
8 months ago
1 1

Hello,

How do I import a script to a collection to use inside a hook? For example

/* collections > Product.js */

/* Importing someting*/
import { script } from "../utils/scripts";

/* Creating a collection */
const Product = {
    slug: 'products',
    ...
    hooks: {
        beforeChange: [
            async () => { script(); }
       ]
}

When I build this it builds ok, but when I am trying to access the admin panel it says in the console "webpack_require(...).config is not a function". And when I remove the imports, it works fine.

Any ideas how can I make this work?

Thank you!

  • vstarush
    8 months ago

    Figured it out. Fixed it by changing "require" to "import" in the script.

Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More