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

Failed to bundle npm as a dependency: Cannot find module 'node-gyp/bin/node-gyp' #275

Closed
styfle opened this issue Feb 9, 2019 · 4 comments · Fixed by #287
Closed

Failed to bundle npm as a dependency: Cannot find module 'node-gyp/bin/node-gyp' #275

styfle opened this issue Feb 9, 2019 · 4 comments · Fixed by #287

Comments

@styfle
Copy link
Member

styfle commented Feb 9, 2019

I can't seem to bundle npm as a runtime dependency.

This is necessary to get Package Phobia on Now 2.0.

Steps to reproduce

git clone https://github.com/styfle/ncc-bug-npm
cd ncc-bug-npm
ncc build index.js
node dist/index.js

Error output

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'node-gyp/bin/node-gyp'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at resolveFileName (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:95521:39)
    at resolveFrom (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:95535:9)
    at module.exports.module.exports (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:95538:41)
    at Object.<anonymous> (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:33356:31)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
    at Object.<anonymous> (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:47941:19)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
    at Object.<anonymous> (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:73093:32)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
    at Object.module.exports.module.exports (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:92854:14)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
    at Object.<anonymous> (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22110:19)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
    at Object.<anonymous> (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:57109:13)
    at __webpack_require__ (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:22:30)
@styfle styfle changed the title Failed to bundle npm as a dependency due to node gyp Failed to bundle npm as a dependency: Cannot find module 'node-gyp/bin/node-gyp' Feb 9, 2019
@styfle
Copy link
Member Author

styfle commented Feb 9, 2019

I also have a branch called npm-commands in the same repo that gets a different error:

/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:28613
	throw e;
	^

Error: Cannot find module '/Users/styfle/Desktop/ncc-bug-npm/dist/lib/install.js'
    at webpackEmptyContext (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:28611:10)
    at Object.get [as install] (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:33054:41)
    at EventEmitter.npm.load (/Users/styfle/Desktop/ncc-bug-npm/dist/index.js:40778:26)
    at process._tickCallback (internal/process/next_tick.js:61:11)

@guybedford
Copy link
Contributor

I just tried this out and get a very different error:

node dist/index.js
npm WARN saveError ENOENT: no such file or directory, open '/tmp/copee/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/tmp/copee/package.json'
npm WARN copee No description
npm WARN copee No repository field.
npm WARN copee No README data
npm WARN copee No license field.

+ copee@1.0.6
updated 1 package in 0.102s
Success

It says Success right!?

Are you sure you are using ncc 15? We can certainly try to continue working on this though.

@styfle
Copy link
Member Author

styfle commented Feb 14, 2019

@guybedford The build works fine. It's when you try to run the output file is when it fails.

Notice the last step in the repro steps above is node dist/index.js.

I just tested again with ncc 15 and its the same error.

I should be more clear: this is a runtime bug, not a compile time bug.

@guybedford
Copy link
Contributor

@styfle ok this is because the code that loads node-gyp is the following:

const DEFAULT_NODE_GYP_PATH = resolveFrom(__dirname + '/npm-lifecycle', 'node-gyp/bin/node-gyp')

where resolveFrom is the custom method defined to contain:

const resolveFileName = () => Module._resolveFilename(moduleId, {
		id: fromFile,
		filename: fromFile,
		paths: Module._nodeModulePaths(fromDir)
	});

so the static analysis just doesn't reach this level unfortunately.

The best thing to do here would be to inline the actual resolveFrom npm package implementation into ncc itself as a specific static analysis that is done to fix this.

Note that you can mimic this already by just ensuring that node-gyp is installed in the output folder -
node_modules/node-gyp. Then you get the other error pointed to above, which is the really interesting obscure bug next :)

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

Successfully merging a pull request may close this issue.

2 participants