Skip to content

[Bug] Global installation with npm fails due to SyntaxError in postinstall.js #119

@asdasdssdd112

Description

@asdasdssdd112

Hello,

I'm encountering an issue when trying to install opencode-ai globally using npm. The installation process fails consistently with a SyntaxError.

Steps to Reproduce

  1. Ensure you have a recent version of Node.js and npm installed (e.g., Node.js v20).
  2. Open any terminal (PowerShell, Command Prompt, bash, zsh, etc.).
  3. Run the command:
    npm i -g opencode-ai@latest

Expected Behavior

The package should install successfully without any errors, making the opencode command available in the system's PATH.

Actual Behavior

The installation fails during the postinstall script. The following error is displayed:

npm error code 1
npm error path C:\Users\gebruiker 10\AppData\Roaming\npm\node_modules\opencode-ai
npm error command failed
npm error command C:\Windows\system32\cmd.exe /d /s /c node ./postinstall.js
npm error (node:24292) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
npm error C:\Users\gebruiker 10\AppData\Roaming\npm\node_modules\opencode-ai\postinstall.js:3
npm error import fs from "fs"
npm error ^^^^^^
npm error
npm error SyntaxError: Cannot use import statement outside a module
npm error     at wrapSafe (node:internal/modules/cjs/loader:1378:20)
npm error     at Module._compile (node:internal/modules/cjs/loader:1428:41)
npm error     at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
npm error     at Module.load (node:internal/modules/cjs/loader:1288:32)
npm error     at Module._load (node:internal/modules/cjs/loader:1104:12)
npm error     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
npm error     at node:internal/main/run_main_module:28:49
npm error
npm error Node.js v20.17.0

Environment

  • Node.js Version: v20.17.0
  • npm Version: (The version bundled with Node v20.17.0)
  • Operating System: Windows 11 (tested in PowerShell) and Ubuntu (tested in WSL). The error is identical on both, confirming it's not an OS-specific issue.

Analysis & Suggested Fix

The error SyntaxError: Cannot use import statement outside a module clearly indicates that the postinstall.js script is using ES Module import syntax. However, the root package.json for this project does not contain "type": "module", so Node.js attempts to execute the script as a CommonJS module by default, which causes the syntax error.

There are a few ways to fix this:

  1. Change the syntax in postinstall.js to use CommonJS:
    // Change this:
    import fs from "fs"
    
    // To this:
    const fs = require("fs") 
  2. Add "type": "module" to the project's package.json file.
  3. Rename the script from postinstall.js to postinstall.mjs to signal to Node.js that it is an ES Module.

Thank you for looking into this

Metadata

Metadata

Assignees

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