-
Notifications
You must be signed in to change notification settings - Fork 187
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
Description
Environment
- OS: Windows (native, not WSL)
- Node.js: v22.x
- Tool: Claude Code (skill/plugin auto-execution)
- Repo:
apify/agent-skills - Doc referenced:
README.mdhttps://github.com/apify/agent-skills/blob/main/README.md - Date: 2026-03-11
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
- On Windows (native), install Node.js 22.x.
- In Claude Code, install/use the Apify Agent Skills and trigger the
apify-trend-analysisskill so Claude Code executes it automatically. - 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/..."
- (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/..."
- 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-analysisskill 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
.envmust 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.jsis written as an ESM module, but it is being executed in a CommonJS context:- No
package.jsonwith"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.
- No
- The use of
--env-file=.envis cwd-dependent; Claude Code’s execution cwd may differ from the user’s project directory.
Workarounds (Temporary)
- Add a
package.jsoncontaining:into the directory:{ "type": "module" }
...\reference\scripts\
so Node treatsrun_actor.jsas 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.envin cwd.
Suggested Fix
Any of the following would address the ESM crash:
- Ship a
package.jsonwith"type": "module"in the appropriate scope forreference/scripts/run_actor.js. - Rename
run_actor.jstorun_actor.mjsand update the invocation accordingly. - Rewrite the runner as CommonJS (
require) to avoid ESM scope issues (less preferred).
And for token loading reliability:
- Avoid cwd-relative
--env-file=.envby default (or clearly document required cwd / expected.envlocation), or - Default to
~/.claude/.envon all platforms.