Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The NodeJS program running result is different in Github Action SSH Remote Commands #363

Open
hh54188 opened this issue Jan 23, 2025 · 1 comment

Comments

@hh54188
Copy link

hh54188 commented Jan 23, 2025

In a NodeJS program I use the ES module by specifying the type field inside the project. Also I created an ecosystem.config.js for PM2 usage purposes. But because the PM2 still using the CommonJS module, I gave it a cjs suffix

This is the ecosystem.config.js content:

module.exports = {
    apps: [{
        name: "app-name",
        script: "./index.mjs",
    }]
}

This is the folder structure:

project-root/
├── node_modules/         
├── .env                 
├── .gitignore          
├── ecosystem.config.cjs 
├── index.mjs           
└── package.json        

And I deploy the project to the DigitalOcean Droplet via Github Action SSH Remote Commands

  - name: SSH Remote Commands
    uses: appleboy/[email protected]
    with:
      host: xxx.xxx.xxx.xxx
      username: root
      key: ${{ secrets.SSH_KEY }}
      script: |
        pm2 stop ${{ env.REPO_NAME }}
        pm2 flush ${{ env.REPO_NAME }}
        pm2 start ecosystem.config.cjs
        exit

After deployment I got this error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/nodejs-app/index.mjs
    at Module.load (internal/modules/cjs/loader.js:861:11)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_REQUIRE_ESM'
}

However, if I manually run the PM2 command in the VPS by myself everything would be fine. What’s wrong with the Github Action

@appleboy
Copy link
Owner

Try: #21 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants