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

npx resolve errors #315

Closed
vvscode opened this issue Sep 14, 2023 · 17 comments
Closed

npx resolve errors #315

vvscode opened this issue Sep 14, 2023 · 17 comments

Comments

@vvscode
Copy link

vvscode commented Sep 14, 2023

I have the error, so I added some debug info upfront:

console.log({
    npm_lifecycle_script: process.env.npm_lifecycle_script,
    _: process.env._,
    __filename
})

if (
    String(process.env.npm_lifecycle_script).slice(0, 8) !== 'resolve '
    && (
        !process.argv
        || process.argv.length < 2
        || (process.argv[1] !== __filename && fs.statSync(process.argv[1]).ino !== fs.statSync(__filename).ino)
        || (process.env._ && path.resolve(process.env._) !== __filename)
    )
) {
    console.error('Error: `resolve` must be run directly as an executable');
    process.exit(1);
}

that gives me:

{
  npm_lifecycle_script: 'resolve',
  _: '/Users/v.vanchuk/repo/test/node_modules/.bin/resolve',
  __filename: '/Users/v.vanchuk/repo/test/node_modules/resolve/bin/resolve'
}

I failed to follow the logic in this place

  • when npm_lifecycle_script can have space at the end?
  • why in general you make this restriction? (what's the purpose?)

Hope you can help / clarify this moment =)

Thanks in advance

@ljharb
Copy link
Member

ljharb commented Sep 14, 2023

I'm a bit confused, can you clarify how you're getting this error?

@vvscode
Copy link
Author

vvscode commented Sep 14, 2023

Sure =)

I just created new empty project

{
  "name": "resolve-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "resolve": "^1.22.4"
  }
}

and installed dependencies with yarn

and inside I do run npx resolve

resolve-demo.zip

@ljharb
Copy link
Member

ljharb commented Sep 14, 2023

aha, thanks :-) so, the bug is that it doesn't work with npx.

@ljharb ljharb changed the title Error: resolve must be run directly as an executable npx resolve errors Sep 14, 2023
@vvscode
Copy link
Author

vvscode commented Sep 14, 2023

I'm happy to help with the fix if you clarify the logic =)

shouldn't

String(process.env.npm_lifecycle_script).slice(0, 7) !== 'resolve'

or even

process.env.npm_lifecycle_script !== 'resolve'

solve the issue? (I didn't get that space at the end at all)

@vvscode

This comment was marked as outdated.

@ljharb ljharb closed this as completed in 6f96aee Sep 14, 2023
@ljharb
Copy link
Member

ljharb commented Sep 14, 2023

There'll be no need for that workaround; i'll have a release cut momentarily.

ljharb added a commit that referenced this issue Sep 14, 2023
ljharb added a commit that referenced this issue Sep 14, 2023
 - [Fix] fix `npx resolve` by handling symlinks (#315)
 - [Dev Deps] update `array.prototype.map`
ljharb added a commit that referenced this issue Sep 14, 2023
v1.22.5

 - [Fix] fix `npx resolve` by handling symlinks (#315)
 - [Dev Deps] update `array.prototype.map`
@ljharb
Copy link
Member

ljharb commented Sep 14, 2023

v1.22.5 is published.

@vvscode
Copy link
Author

vvscode commented Sep 15, 2023

it works. Thanks

@vvscode
Copy link
Author

vvscode commented Sep 15, 2023

@ljharb just for my understanding - is this limitation to have correct argv and have no errors on processing params?

@ljharb
Copy link
Member

ljharb commented Sep 15, 2023

And to ensure it can’t be required/imported.

@vvscode
Copy link
Author

vvscode commented Sep 18, 2023

function isEntryPoint() {
  return require.main === module;
}

might be more relyable solution for this

@ljharb
Copy link
Member

ljharb commented Sep 18, 2023

require.main is deprecated and something that should never, ever have existed; i'm not going to use it.

@vvscode
Copy link
Author

vvscode commented Sep 18, 2023

Can you link some information about it?
I checked node@20 docs and see it is there without any deprecation notes

https://nodejs.org/api/modules.html#accessing-the-main-module

@vvscode
Copy link
Author

vvscode commented Sep 18, 2023

More than that

https://nodejs.org/api/deprecations.html#dep0138-processmainmodule

DEP0138: process.mainModule#

History
Version Changes
v14.0.0 Documentation-only deprecation.

Type: Documentation-only

process.mainModule is a CommonJS-only feature while process global object is shared with non-CommonJS environment. Its use within ECMAScript modules is unsupported.

It is deprecated in favor of require.main, because it serves the same purpose and is only available on CommonJS environment.


DEP0138: process.mainModule[#](https://nodejs.org/api/deprecations.html#dep0138-processmainmodule) History Version Changes v14.0.0 Documentation-only deprecation.

Type: Documentation-only

process.mainModule is a CommonJS-only feature while process global object is shared with non-CommonJS environment. Its use within ECMAScript modules is unsupported.

It is deprecated in favor of require.main, because it serves the same purpose and is only available on CommonJS environment.

@vvscode
Copy link
Author

vvscode commented Sep 21, 2023

@ljharb FYI^^

@ljharb
Copy link
Member

ljharb commented Sep 21, 2023

I'm not going to use require.main ever, sorry. The current heuristic is my preferred approach.

@vvscode
Copy link
Author

vvscode commented Sep 21, 2023

I proposed once, this mention was just to inform you about the state of that API. Thanks for helping me with the issues

Good luck

ljharb added a commit that referenced this issue Oct 10, 2023
Changes since v2.0.0-next.4:

 - [Fix] fix `npx resolve` by handling symlinks (#315)
 - [Fix] allow `npx resolve` to work (#316)
 - [Tests] rename innocent test project to avoid flawed security scanners
 - [Tests] avoid publishing "malformed package.json" test to avoid flawed security scanners

Including all changes in v1.20.2 - v1.22.8

v1.22.8
 - [Tests] rename innocent test project to avoid flawed security scanners

v1.22.7

 - [Tests] avoid publishing "malformed package.json" test to avoid flawed security scanners

v1.22.6

 - [Fix] allow `npx resolve` to work (#316)
 - [actions] use reusable rebase action

v1.22.5

 - [Fix] fix `npx resolve` by handling symlinks (#315)
 - [Dev Deps] update `array.prototype.map`

v1.22.4

 - Revert "[Refactor] deprecated `lib/core.js` now uses data from `is-core-module` also"
 - [Deps] update `is-core-module`
 - [Dev Deps] update `@ljharb/eslint-config`, `aud`, `semver`, `tape`
 - [meta] commit published core.json data

v1.22.3

 - [Refactor] deprecated `lib/core.js` now uses data from `is-core-module` also
 - [Deps] update `is-core-module`

v1.22.2

 - [Refactor] deprecated `lib/core.js` now uses `is-core-module`
 - [meta] cp core.json on prepack instead of prepublishOnly
 - [Deps] update `is-core-module`
 - [Dev Deps] update `@ljharb/eslint-config`, `aud`, `tape`, `array.prototype.map`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants