sezna / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json
MIT License
1.43k stars 93 forks source link

Using `package-scripts.js` in ESM context? #230

Open ctjlewis opened 3 years ago

ctjlewis commented 3 years ago

Sadly, I have the great misfortune of needing to work on a codebase which uses nps, and despite the project's stated goal, "unmaintainable mess" is quite an understatement due to the number of security vulnerabilities and CJS/ESM conflicts I'm getting.

Is there any way for me to load package-scripts.js in an ESM context? It seems like nps is just trying to naively require(...) the module without checking for mjs extension, so nps -c ./package-scripts.mjs throws ERR_REQUIRE_ESM.

brekk commented 3 years ago

@ctjlewis Have you tried using the .cjs extension? I was having the same problem but I was able to get things working by changing the package-scripts.js file to package-scripts.cjs and then calling nps with the command:

nps -c ./package-scripts.cjs

Definitely not as simple as just typing nps of course, but I aliased the above in my bash profile and that's at least a workable answer (for me).

jcubic commented 3 years ago

I have similar problem, also the old project that use nps. But in my case nps run other nps scripts and nps -c ./package-scripts.cjs doesn't work because nested nps are no invoked I've got error that no config get found. Only top level nps is executed.

SupremeTechnopriest commented 3 years ago

You have a few options:

  1. Use yaml instead of json package-scripts.yaml
  2. Add a .npmrc.json with the following config and rename package-scripts.js to package-scripts.cjs
{
  "config": "./package-scripts.cjs"
}

I use the .cjs option.