I have a shared module called foo that I include in my project's node_modules directory via npm link. In the foo module, I have a require('react') statement where 'react' is included as a peer dependency in my project's node_modules. When using Browserify 9.0.8 everything works fine, but upgrading to 11.0.0 I get the error Cannot find module 'react'.
I'm experiencing this exact same issue. In my node projects, when using npm link, I've always had to use the parent-require package to get past node's inability to use symlinked modules. It was always a blessing that this wasn't an issue with browserify, but this no longer seems to be the case.
To clarify, I use grunt-browserify and the issue arose when upgrading from [email protected] to 3.8.0 which updated the browserify dependency from ^9.0.8 to ^10.0.0.
Also having a similar issue. I can fix it by removing this change, does that work for you? https://github.com/substack/node-browserify/pull/1318/files. Could give us insight into a solution
this should fix the problem, i.e. symlink react inside foo to the react that is inside your main app
node_modules
foo -> ../../foo
react
foo
node_modules
react -> ../../app/node_modules/react
Also having this issue.
My guess is that this is the offending line: https://github.com/substack/node-browserify/pull/1318/files#diff-168726dbe96b3ce427e7fedce31bb0bcR485
Previously it would pass the path as it were in your projects node_modules, but after this change the realpath is passed instead. When the realpath is later passed on to the node-resolve module, any require()-d modules are resolved by traversing upwards in the directory hierarchy from the realpath instead of the symlinked path.
~/projects
|-- my-project
|-- node_modules
|-- my-module (symlink pointing at ~/projects/my-module)
|-- index.js
> require('react')
|-- react
|-- my-module
|-- index.js
The path for my-module/index.js as passed on to node-resolve would after the edit referenced above be:
~/projects/my-module/index.js
...while it should be:
~/projects/my-project/node_modules/my-module/index.js
...for node-resolve to be able to resolve react
Just realized by looking at #1325, that this matches what happens with symlinked modules in node, so I guess @cvlmtg's workaround is the pragmatic solution here.
My solution was to build using an npm script like:
"build:js": "NODE_PATH=./node_modules browserify ..."
which means that the react module (and any other peer dependencies) from your app is found when requires (or ES6 imports) happen in the npm linked module(s). Probably doesn't work on Windows, but adding a 'set' or something would probably fix it. It's a hack, and you should check things do still work when you copy in the module to the node_modules and don't use NODE_PATH=.
@tomwidmer +1 - this does appear to work, thanks!
Same problem, and somehow @tomwidmer 's hack doesn't work for me. I tried both the original NODE_PATH=./node_modules and also the absolute path.
browserify v16.5.0, Node.js v8.16.2, npm v6.9.0
Most helpful comment
My solution was to build using an npm script like:
which means that the react module (and any other peer dependencies) from your app is found when requires (or ES6 imports) happen in the npm linked module(s). Probably doesn't work on Windows, but adding a 'set' or something would probably fix it. It's a hack, and you should check things do still work when you copy in the module to the node_modules and don't use
NODE_PATH=.