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

Error during "yarn pack": Cannot find module "." #6794

Closed
mariandev opened this issue Dec 4, 2018 · 10 comments
Closed

Error during "yarn pack": Cannot find module "." #6794

mariandev opened this issue Dec 4, 2018 · 10 comments
Assignees
Labels

Comments

@mariandev
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Running the command yarn pack throws an error if there are any packages in bundledDependencies

Error: Cannot find module "."
    at webpackMissingModule (/usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:109772:64)
    at pkg (/usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:109772:142)
    at again (/usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:109727:22)
    at depsFor (/usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:109741:4)
    at /usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:42211:30
    at Generator.next (<anonymous>)
    at step (/usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:304:30)
    at /usr/local/Cellar/yarn/1.12.3/libexec/lib/cli.js:315:13
    at <anonymous>

I tracked the bug to this section of code, more specifically the line with the thePackage assign

/* 725 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


var resolvePkg = __webpack_require__(726);

/* @private
 *
 * given the name of a descendent module this module locates and returns its
 * package.json. In addition, it provides the baseDir.
 *
 * @method pkg
 * @param {String} name
 * @param {String} dir (optional) root directory to begin resolution
 */
module.exports = function pkg(name, dir) {
  if (name !== './') { name += '/'; }

  var packagePath = resolvePkg(name, dir);
  if (packagePath === null) { return null; }

  var thePackage = !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }());

  thePackage.baseDir = packagePath.slice(0, packagePath.length - 12 /* index of `/package.json` */);

  return thePackage;
};


/***/ }),

If the current behavior is a bug, please provide the steps to reproduce.

  • yarn init -y
  • yarn add typescript
  • Add "bundledDependencies": ["typescript"] to the package.json
  • yarn pack

What is the expected behavior?
An archive is created containing the specified module.

Please mention your node.js, yarn and operating system version.
Node 8.12.0
Yarn 1.12.3
MacOS 10.14.1

@ghost ghost assigned torifat Dec 4, 2018
@ghost ghost added the triaged label Dec 4, 2018
@mariandev
Copy link
Author

mariandev commented Dec 4, 2018

I tracked that piece of code to this https://github.com/stefanpenner/hash-for-dep/blob/master/lib/pkg.js . It looks like webpack tries to resolve that require statement and that is not right.

@arcanis
Copy link
Member

arcanis commented Dec 5, 2018

I'd personnally advise you not to use bundled dependencies, ever, whether it's on Yarn or any other package manager. They are a badly designed feature 😕

@mariandev
Copy link
Author

Ok, thanks for the advice.

I was trying to make a custom piece of code that did exactly this and then I found that yarn had this functionality already built-in. It was sad to find out that it doesn't work :(

Still, it would be great if this bug gets fixed, because the pack command is kinda useless without the bundledDependencies imho. Or the command should be removed altogether.

@payload
Copy link

payload commented Jul 29, 2019

I ran into this issue. npm pack bundledDependencies resolution does not work with yarn workspaces hoisted node_modules. yarn pack just fails with this not helpful error message 'Cannot find module "."'.
I ended up writing my own piece of code to bundle things up. Made mistakes there too 😅

@GusGold
Copy link

GusGold commented Aug 22, 2019

And if you fix the broken webpack require, yarn will pack the package without including any bundledDependencies

@payload Would you be able to share your custom solution?

@payload
Copy link

payload commented Aug 22, 2019

@GusGold unfortunately not. It's actually fun to write such a bundle script, but you could also just fairly straight forward write a script which does all the manual steps hardcoded. npm pack relevant dependencies, create a zip archive, put all the files and dependencies as tgz in, rewrite package.json dependencies to use those tgz.

@GusGold
Copy link

GusGold commented Aug 22, 2019

No worries, @payload . I started writing a very similar solution last night, but was trying to emulate yarn's cache and then using yarn install --cache-folder [custom-packed-modules] but wasn't able to consistantly trick yarn into using them.

I hadn't considered rewriting the package.json to look directly for the tgz, thanks!

honzajavorek added a commit to apiaryio/dredd that referenced this issue Oct 18, 2019
This improves the hack which gets rid of protagonist (i.e. C++ compilation
of drafter, the API Blueprint parser). When we moved dredd-transactions
to the monorepo, where dependencies are managed by yarn workspaces,
the hack to get rid of protagonist stopped to work. The hack relies on
'bundledDependencies', but those stopped to work. 'npm pack' cannot find
the dependencies to bundle if they're in the root 'node_modules' directory.
'yarn pack' doesn't work either, it has bugs and it doesn't support
'bundledDependencies' or the workspaces very well - see yarnpkg/yarn#6794

This change overcomes the limitation by improving the prepack.js script.
It now iterates over the 'bundledDependencies' and symlinks them to the
local 'node_modules' directory where 'npm pack' can pick them up. After
packing is done, there's a new postpack.js script, which makes sure the
symlinks get cleaned up. The resulting .tgz then contains the dependencies
bundled - without protagonist.
honzajavorek added a commit to apiaryio/dredd that referenced this issue Oct 18, 2019
This improves the hack which gets rid of protagonist (i.e. C++ compilation
of drafter, the API Blueprint parser). When we moved dredd-transactions
to the monorepo, where dependencies are managed by yarn workspaces,
the hack to get rid of protagonist stopped to work. The hack relies on
'bundledDependencies', but those stopped to work. 'npm pack' cannot find
the dependencies to bundle if they're in the root 'node_modules' directory.
'yarn pack' doesn't work either, it has bugs and it doesn't support
'bundledDependencies' or the workspaces very well - see yarnpkg/yarn#6794

This change overcomes the limitation by improving the prepack.js script.
It now iterates over the 'bundledDependencies' and symlinks them to the
local 'node_modules' directory where 'npm pack' can pick them up. After
packing is done, there's a new postpack.js script, which makes sure the
symlinks get cleaned up. The resulting .tgz then contains the dependencies
bundled - without protagonist.
@zypA13510
Copy link

zypA13510 commented Jan 16, 2020

Since it's impossible to test this now that yarn pack is broken, please also make sure to pack any symlinked bundledDependencies first before bundling. npm link messed this up, it caused the "files" field in manifest or an .npmignore file to be ignored and the entire symlink source folder to be bundled. I hope yarn will not repeat this mistake.

@chucknelson
Copy link

Not sure if this particular issue is fixable at all, but ran into it setting up some simple testing to (hopefully) contribute a fix for #5998.

With yarn v1 sticking around as the "npm compatible" option compared to yarn v2 / "berry", maybe we can start fixing up some of these bundledDependencies issues, even though they aren't the most popular :) Going to see if I can actually do something with #5998 first...

@paul-soporan
Copy link
Member

Closing as we've completely removed support for bundleDependencies in v2, for the reasons stated here https://yarnpkg.com/getting-started/migration#dont-use-bundledependencies. We aren't going to spend more time fixing them in v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants