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

Unable to locate correctly the package.json to identify if the script is an ESModule/CommonJS #5266

Closed
THernandez03 opened this issue Jan 11, 2022 · 0 comments

Comments

@THernandez03
Copy link
Contributor

THernandez03 commented Jan 11, 2022

What's going wrong?

Context:

When we define the script param, PM2 have to identify if this file it's an ESModule or CommonJS, to do so, it tries to find the package.json from the current directory of the script. If it's not there, it will check on the parent one. (ProcessUtils.js).

If the file is located, It will look for the { type: "module" } property, so If it's a "module" then it loads as an ESModule otherwise, will be loaded as a CommonJS module

Problem:

If we have different folder structure something like

project
|--> package.json
|--> src
       |--> browser
       |--> shared
       |--> server
              |--> server.js

PM2 will be unable to find the package.json unless we move the script into src or project folder.

How could we reproduce this issue?

I created a CodeSandbox.
In a new terminal, you can run any of the following commands

  1. npm run start1: This will be able to run the server, locating correctly the package.json
  2. npm run start2: This will not be able to run the server, because pm2 is unable to locate correctly the package.json and will understand server.js as a CommonJS instead of a ESModule.

Supporting information

Using .mjs extension will identify the module correctly, but the Node Specification also accepts this approach Files ending in .js when the nearest parent package.json file contains a top-level "type" field with a value of "module".

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

1 participant