Esm: pm2 script start

Created on 29 Mar 2018  路  4Comments  路  Source: standard-things/esm

Having a bit of trouble getting pm2 to start my app. Little help please.

First off this works fine
/opt/wifi/server$ node -r @std/esm sbc

But when try to run it form a pm2 config.js file it says it can't find the script.
```
{
name : 'wifi-server',
cwd : '/opt/wifi/server',
script : 'sbc --node-args="-r @std/esm"',
watch : true
}

[PM2][ERROR] script not found : /opt/wifi/server/sbc --node-args="-r @std/esm"

guess it's not really recognizing the sbc.mjs file maybe cause it's not really using @std/esm? and thus doesn't know what an .mjs file is.

Does @std/esm need to be loaded globally? It's only in the node_modules folder of /opt/wifi/server.
but I thought setting the cwd would take care of that

question

Most helpful comment

Hi @dkebler!

I haven't worked with a pm2 config (only command-line).
I'm guessing it has something to do with which directory the config is using to resolve packages.

This works for me

npx pm2 start main.js --node-args="-r esm"

Note: btw @std/esm is discontinued in favor of esm.

Update:

It looks like the config should have a node_args field.

{
  "node_args": "-r esm"
}

All 4 comments

Hi @dkebler!

I haven't worked with a pm2 config (only command-line).
I'm guessing it has something to do with which directory the config is using to resolve packages.

This works for me

npx pm2 start main.js --node-args="-r esm"

Note: btw @std/esm is discontinued in favor of esm.

Update:

It looks like the config should have a node_args field.

{
  "node_args": "-r esm"
}

For any finding this here is solution

per @jdalton I loaded create-esm in my project. (no need for global)

Then this was the script that worked below. Note that first app does not need esm but the second does. Unlike a regular cl start here you MUST include the .mjs. I guess pm2 won't search .mjs files by default (is that an issue to be raised for PM2?) like dalton's esm does at a simple cl invocation

module.exports = {
  /**
   * Application configuration section
   * http://pm2.keymetrics.io/docs/usage/application-declaration/
   */
  apps : [
    // Start Client
    {
      name      : 'wifi-client',
      args      : '/opt/wifi/client/dist/spa-mat',
      script    : '/opt/npm-global/bin/http-server',
      watch     : true
    },
    // Start Server
    {
      name      : 'wifi-server',
      node_args : '-r esm',
      cwd       : '/opt/wifi/server',
      script    : '/opt/wifi/server/sbc.mjs',
      watch     : true
    }
  ]
}

Starting another issue but create-esm is not working for me on the same code

The simple solution is to use .js for ESM to get things to just work with today's projects. .mjs is going to be the least working option since it's locked down and no esm options apply to it.

Was this page helpful?
0 / 5 - 0 ratings