2

I am trying to create my very first node package, it was successfully publish and live on npmjs.com website but whenever i include it on a sample react project using create-react-app i get this following error

Here's how i import it

import IconTooltip from 'react-icons-tooltip';

Here's the error

Module not found: Can't resolve './react-icons-tooltip' in '/home/ubuntu/workspace/lasting/src/Components'

This is the Package package.json file

{
  "name": "react-icons-tooltip",
  "version": "1.0.4",
  "description": "",
  "main": "./lib/IconTooltip.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --progress --colors --p",
    "transpile": "node_modules/.bin/babel ./src/index.js --presets babel-preset-es2015 --out-file lib/IconTooltip.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.3",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.10"
  },
  "dependencies": {
    "prop-types": "^15.6.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  }

}

webpack.config.js

const webpack = require("webpack");

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname + '/build',
        filename: 'IconToolTip.js'
    },
    module: {
        rules: [
            {
              test: /\.js$/,
              exclude: /node_modules/,
              loader: 'babel-loader'
            },
            {
              test: /\.css$/,
              loaders: ['style-loader', 'css-loader']
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        })
    ]
};

If i import the local the package seems to work fine

import IconTooltip from './lib/IconTooltip';

TIA!

  • If it's on the npm registry, you should install it: `npm install react-icons-tooltip`, then import it: `import IconTooltip from 'react-icons-tooltip'` (Notice you have to import from `node_modules`, not from the same directory as you're doing right now). – Miguel Calderón Mar 02 '18 at 10:07
  • @MiguelCalderón Sorry i mistakenly put ',/' i am importing it just like what you said. –  Mar 02 '18 at 10:09
  • 1
    I also tested importing the local version and it worked. `import IconTooltip from './lib/IconTooltip';` but not `import IconTooltip from 'react-icons-tooltip'` after i install it –  Mar 02 '18 at 10:11
  • In your `weback.config.js`file, in the `output` section, make sure you set these fields as follows: `filename: 'index.js'` and `libraryTarget: 'commonjs2'`. – Miguel Calderón Mar 02 '18 at 11:07
  • @MiguelCalderón wow that fixed my problem... does the output necessarily need to be index.js? thank u –  Mar 02 '18 at 12:13

1 Answers1

-2

I had that same problem before and that fixed it. I checked other modules and they all enter in index.js, is all I can say :)

Miguel Calderón
  • 3,001
  • 1
  • 16
  • 18