3

I am developping a library that needs to work in both node and a react app. I use a webpack 4 project that generates a UMD module, but I have a problem when I try to import it in a simple webpack project.

When I use import lib from 'myLib'; lib is undefined

This is what my lib look like

export const printMsg = function() {
  return "This is a message from the lib";
};

With this webpack configuration

const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "mylib.js",
    library: "mylib",
    libraryTarget: "umd",
    umdNamedDefine: true,
    globalObject: `(typeof self !== 'undefined' ? self : this)`
  }
};

for now I'm using a boilerplate project (https://github.com/wbkd/webpack-starter) to test.

I used npm install --save mylib to add it, and my dist code is inside my node_modules folder. My index.js file look like this :

import "../styles/index.scss";
import lib from "watson-tile-lib";
console.log(lib);

When I run the page I got undefined in my browser console.

Any idea ?

Evilgalith
  • 31
  • 4

1 Answers1

0

what is your main field in package.json? It should be the builded target like:

main: "dist/mylib.js"