webpack: Modules fail to load inside user-scoped modules `require` statements

If I depend on a a user-scoped module (i.e. named @username/user-scoped-module-name), the modules it depends on fail to load with the error:

Error: Cannot find module 'underscore'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> ([..]\webpack:\external "underscore":1:1)
    at __webpack_require__ ([..]\webpack:\webpack\bootstrap 50dd70b4bf3911f26d28:19:1)
    at Object.<anonymous> ([..]\webpack:\~\@eventuateinc\eventuate-nodejs-client\modules\es.js:5:1)
    at __webpack_require__ ([..]\webpack:\webpack\bootstrap 50dd70b4bf3911f26d28:19:1)
    at Object.module.exports.exports.entityTypeName ([..]\webpack:\[..]\src\modules\EsClient.js:1:28)
    at __webpack_require__ ([..]\webpack:\webpack\bootstrap 50dd70b4bf3911f26d28:19:1)

This does not apply neither to nodejs internal modules (e.g. ‘utils’, etc.) nor to locally available files. So if the mentioned user-scoped module has code like this…:

/* app.js inside the '@username/user-scoped-module-name' package */
var util = require('util');
var stomp = require('./stomp/stomp');
var _ = require('underscore');

… the exception will get thrown only for the ‘underscore’ reference (third require statement).

I believe the cause of this issue lies in a way the user-scoped modules are loaded into a file system. They reside not in the folder that’s an immediate child to node_modlues folder but within an extra folder named @username:

/node_modules
^---/@username
    ^----/user-scoped-module-name
        .package.json
^---/plain-module-name

A simple hack to solve this (NOTE, not a proposed solution!) would be to:

  1. (after npm install) copy or move the user-scoped-module-name folder (in the illustration above) one level up,
  2. reference it in the package.json as user-scoped-module-name instead of @username/user-scoped-module-name
  3. require/import it based on its new name (user-scoped-module-name).

But this really is a hack in the place where it should work without these atrocities.

Please consider assigning it a high priority, as it fails to support basic npm mechanisms, not something advanced.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 19
  • Comments: 29 (6 by maintainers)

Most upvoted comments

Any updates on this? It’s a big issue for me as well.

Any update here ?

Same here: if a require a file which in turn is requiring a scoped package then the scoped package is not found!

//in SimpleForm.js
import Input from '../src/Input';

//in ../src/Input.js
import update from '@damianobarbati/react-update';

ERROR in ../src/Input.js
Module not found: Error: Can't resolve '@damianobarbati/react-effect' in '/Users/damz/Desktop/react-redux-form/src'
 @ ../src/Input.js 56:0-50
 @ ../index.js
 @ ./SimpleForm.js
 @ ./router.js
 @ ./index.js
 @ multi babel-polyfill ./index.js

I’m amazed there hasn’t been more noise about this, it does seem a significant bug and is causing pain here too.