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
- Ensure you have a recent version of Node.js and npm installed (e.g., Node.js v20).
- Open any terminal (PowerShell, Command Prompt, bash, zsh, etc.).
- 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:
- Change the syntax in
postinstall.js to use CommonJS:
// Change this:
import fs from "fs"
// To this:
const fs = require("fs")
- Add
"type": "module" to the project's package.json file.
- 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
Hello,
I'm encountering an issue when trying to install
opencode-aiglobally usingnpm. The installation process fails consistently with aSyntaxError.Steps to Reproduce
Expected Behavior
The package should install successfully without any errors, making the
opencodecommand available in the system's PATH.Actual Behavior
The installation fails during the
postinstallscript. The following error is displayed:Environment
Analysis & Suggested Fix
The error
SyntaxError: Cannot use import statement outside a moduleclearly indicates that thepostinstall.jsscript is using ES Moduleimportsyntax. However, the rootpackage.jsonfor 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:
postinstall.jsto use CommonJS:"type": "module"to the project'spackage.jsonfile.postinstall.jstopostinstall.mjsto signal to Node.js that it is an ES Module.Thank you for looking into this