Skip to content

apify-trend-analysis fails on Windows (Node 22): run_actor.js uses ESM import but is executed as CommonJS (Cannot use import statement outside a module) #27

@DenisZheng

Description

@DenisZheng

Environment

Summary

When Claude Code auto-runs the apify-trend-analysis skill on Windows, the skill’s reference runner script reference/scripts/run_actor.js crashes immediately with an ESM/CommonJS module loading error. The script uses ESM syntax (import ... from ...), but Node loads it as CommonJS (likely because it is a .js file without a "type": "module" package scope), resulting in:

SyntaxError: Cannot use import statement outside a module

Additionally, the default invocation uses node --env-file=.env (relative path). In the Claude Code execution context, the working directory (cwd) is not obvious/consistent, so .env is frequently not found, which leads to “token not found” / auth failures unless the user manually rewrites the command to an absolute --env-file=... path.

Steps to Reproduce

  1. On Windows (native), install Node.js 22.x.
  2. In Claude Code, install/use the Apify Agent Skills and trigger the apify-trend-analysis skill so Claude Code executes it automatically.
  3. Claude Code runs a command similar to (path may vary, but structure is the same):
    node --env-file=.env "C:/Users/<user>/.claude/plugins/cache/apify-agent-skills/apify-trend-analysis/1.0.1/reference/scripts/run_actor.js" --actor "apify/..."
  4. (Optional) If you attempt to fix the token loading by using an absolute env file path:
    node --env-file=C:/Users/<user>/.claude/.env "C:/Users/<user>/.claude/plugins/cache/apify-agent-skills/apify-trend-analysis/1.0.1/reference/scripts/run_actor.js" --actor "apify/..."
  5. Observe the failure.

Actual Result

The process exits with code 1 and logs:

(node:8736) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
C:\Users\<user>\.claude\plugins\cache\apify-agent-skills\apify-trend-analysis\1.0.1\reference\scripts\run_actor.js:13
import { parseArgs } from 'node:util';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1378:20)
    at Module._compile (node:internal/modules/cjs/loader:1428:41)
...

Separately, with the default --env-file=.env, .env may not be found depending on the cwd, causing token/auth issues before even reaching actor execution.

Expected Result

  • The apify-trend-analysis skill should run successfully on Windows with modern Node (e.g., Node 20+ / 22), without requiring manual patching in a plugin cache directory.
  • Token loading should be reliable (either document exactly where .env must be placed, use a stable default location such as ~/.claude/.env, or avoid a cwd-dependent relative --env-file=.env).

Suspected Root Cause

  • reference/scripts/run_actor.js is written as an ESM module, but it is being executed in a CommonJS context:
    • No package.json with "type": "module" in the script’s package scope, and/or
    • The published/plugin layout causes Node’s module type resolution to treat it as CommonJS.
  • The use of --env-file=.env is cwd-dependent; Claude Code’s execution cwd may differ from the user’s project directory.

Workarounds (Temporary)

  • Add a package.json containing:
    { "type": "module" }
    into the directory:
    ...\reference\scripts\
    so Node treats run_actor.js as ESM. (This is a cache directory, so updates may overwrite it.)
  • Or rename the script to .mjs (also not ideal for a cached/published artifact).
  • For token loading, pass an absolute path to --env-file=... instead of relying on .env in cwd.

Suggested Fix

Any of the following would address the ESM crash:

  1. Ship a package.json with "type": "module" in the appropriate scope for reference/scripts/run_actor.js.
  2. Rename run_actor.js to run_actor.mjs and update the invocation accordingly.
  3. Rewrite the runner as CommonJS (require) to avoid ESM scope issues (less preferred).

And for token loading reliability:

  • Avoid cwd-relative --env-file=.env by default (or clearly document required cwd / expected .env location), or
  • Default to ~/.claude/.env on all platforms.

Reference

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions