Browserify cannot find npm module

Tjorriemorrie

I'm trying to create an NPM module with great pain: react-smallgrid

import React from 'react';
import _ from 'lodash';


export default class SmallGrid extends React.Component{

Compiled with:

browserify: {
    options: {
        transform: [
            ['babelify', {
                presets: ['react', 'es2015']
            }]
        ]
    },

    jsx: {
        files: {
            './dist/js/smallgrid.js': [
                './src/smallgrid.jsx',
            ]
        }
    },

When I import the js file in another project/jsx and try to browserify that, it gives the error:

Error: Cannot find module './ReactMount' from '/Users/me/code/react-smallgrid/dist/js'

I thought it's already compiled for use? I don't understand this.

Meanwhile

I've tried building it with webpack, which gives the following output:

> webpack -p

Hash: 00fd87c95d39230bd485
Version: webpack 1.12.11
Time: 14002ms
       Asset    Size  Chunks             Chunk Names
smallgrid.js  248 kB       0  [emitted]  smallgrid
    + 160 hidden modules

WARNING in smallgrid.js from UglifyJs
Condition always true [./~/react/lib/ReactMount.js:764,0]
Condition always true [./~/react/lib/findDOMNode.js:46,0]
Condition always true [./~/react/lib/instantiateReactComponent.js:80,0]

Still does not work.

inanc

You need to make React libs available to your code.

Run this to add browserify-shim:

npm i browserify-shim -D

Add this to your package.json:

"browserify": {
  "transform": [
    "browserify-shim"
  ]
},
"browser": {
  "react": "./node_modules/react/dist/react.js",
  "react-dom": "./node_modules/react-dom/dist/react-dom.js",
  "lodash": "./node_modules/lodash"
},
"browserify-shim": {
  "./node_modules/react/dist/react.js": "React",
  "./node_modules/react-dom/dist/react-dom.js": "ReactDOM",
  "./node_modules/lodash": "lodash"
}

By the way, you can also use browserify externals in your config to further reduce the resulting package. It's best to not include for example: React in your bundle.


Note:

I also sent you a PR in Github for the solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

browserify cannot find module 'react'

From Dev

browserify exclude cannot find module

From Dev

npm cannot find module

From Dev

Browserify + Grunt + Remapify: Cannot Find Module

From Dev

Gulp Error: Cannot find module browserify

From Dev

Gulp with browserify: Cannot find start module

From Dev

Browserify: Cannot find module '...' from '...' (Coffeescript + React)

From Dev

NPM - Error: Cannot find module '../'

From Dev

Gulp with browserify: Cannot find module src/js/main.js

From Dev

'Cannot find module' error using karma-browserify on Windows

From Dev

Fake global jQuery with browserify-shim? (Cannot find module 'jquery')

From Dev

browserify - uncaught error "cannot find module" in bundle.js

From Dev

karma-browserify Cannot find module on unit test

From Dev

Alias a npm module with browserify shim

From Dev

cannot find module after npm install

From Dev

Error: cannot find module npm-shrinkwrap

From Dev

npm link with webpack - cannot find module

From Dev

Cannot find module 'npmconf' while using npm

From Dev

Receiving 'Error: Cannot find module' using browserify, gulp, react.js

From Dev

Node.js + browserify - Error: Cannot find module 'cls-bluebird'

From Dev

Error: Cannot find module 'npm-registry-client'

From Dev

Cannot find module 'readable-stream' when using npm

From Dev

cannot find module faker after npm install --save-dev

From Dev

node npm install -g cannot find module config-chain

From Dev

Npm install with success but nodejs cannot find module 'db'

From Dev

Running "npm" returns "Error: Cannot find module 'inherits'"

From Dev

npm version shows Error: Cannot find module 'readable-stream'

From Dev

Getting error, Error: Cannot find module ‘express’ after npm install

From Dev

Error "Cannot find module 'npmlog'" after "npm update -g"

Related Related

  1. 1

    browserify cannot find module 'react'

  2. 2

    browserify exclude cannot find module

  3. 3

    npm cannot find module

  4. 4

    Browserify + Grunt + Remapify: Cannot Find Module

  5. 5

    Gulp Error: Cannot find module browserify

  6. 6

    Gulp with browserify: Cannot find start module

  7. 7

    Browserify: Cannot find module '...' from '...' (Coffeescript + React)

  8. 8

    NPM - Error: Cannot find module '../'

  9. 9

    Gulp with browserify: Cannot find module src/js/main.js

  10. 10

    'Cannot find module' error using karma-browserify on Windows

  11. 11

    Fake global jQuery with browserify-shim? (Cannot find module 'jquery')

  12. 12

    browserify - uncaught error "cannot find module" in bundle.js

  13. 13

    karma-browserify Cannot find module on unit test

  14. 14

    Alias a npm module with browserify shim

  15. 15

    cannot find module after npm install

  16. 16

    Error: cannot find module npm-shrinkwrap

  17. 17

    npm link with webpack - cannot find module

  18. 18

    Cannot find module 'npmconf' while using npm

  19. 19

    Receiving 'Error: Cannot find module' using browserify, gulp, react.js

  20. 20

    Node.js + browserify - Error: Cannot find module 'cls-bluebird'

  21. 21

    Error: Cannot find module 'npm-registry-client'

  22. 22

    Cannot find module 'readable-stream' when using npm

  23. 23

    cannot find module faker after npm install --save-dev

  24. 24

    node npm install -g cannot find module config-chain

  25. 25

    Npm install with success but nodejs cannot find module 'db'

  26. 26

    Running "npm" returns "Error: Cannot find module 'inherits'"

  27. 27

    npm version shows Error: Cannot find module 'readable-stream'

  28. 28

    Getting error, Error: Cannot find module ‘express’ after npm install

  29. 29

    Error "Cannot find module 'npmlog'" after "npm update -g"

HotTag

Archive