javascripttypescriptbabeljsrollupjscore-js

module is not defined at node_modules/core-js/internals/global.js


I'm building a library in TypeScript, and I'm trying to test it. As It involves browser API I'm testing it using @web/test-runner and chai.

I'm using TypeScript, and some libraries are commonjs modules, so I'm using Rollup to bundle all of that into something the browser can use.

My rollup config is as follow:

import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { babel } from '@rollup/plugin-babel';
import nodePolyfills from 'rollup-plugin-node-polyfills';

const pkg = require('./package.json');

const libraryName = 'AGreatLibrary';

export default {
  input: 'src/index.ts',
  output: [
    { file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
    { file: pkg.module, format: 'es', sourcemap: true },
  ],
  plugins: [
    json(),
    nodePolyfills(),
    typescript(),
    commonjs(),
    nodeResolve({ browser: true, preferBuiltins: false }),
    sourceMaps(),
    babel({ babelHelpers: 'bundled', exclude: [/\bcore-js\b/, /\bwebpack\/buildin\b/] }),
  ],
};

My babel config

{
  "presets":
  [
      [
          "@babel/env",
          {
              "targets":
              {
                  "edge": "17",
                  "firefox": "60",
                  "chrome": "67",
                  "safari": "11.1"
              },
              "useBuiltIns": "usage",
              "corejs": "3"
          }

      ]
  ],
  "minified": true
}

My test file

import { Client } from "../lib/index.es.js";
import { expect } from '@esm-bundle/chai';

it('Should create a Client', () => {
  let client = new Client("123", true);
  expect(client.accessToken).to.equal("123");
});

it('Should auth', async () => {
  let client = new Client("123", true);
  let resp = await client.tryAuth();
  expect(resp.ok).to.be.true;
});

But when I run my test (web-test-runner "test/**/*.test.ts" --node-resolve)

I get the following error

🚧 Browser logs:
      ReferenceError: module is not defined
        at node_modules/core-js/internals/global.js:6:0

I have no idea what to do.


Solution

  • I fix it!

    plugins: [
        commonjs(),
        json(),
        nodePolyfills(),
        typescript(),
        nodeResolve({ browser: true, preferBuiltins: false }),
        sourceMaps(),
        babel({ babelHelpers: 'bundled', exclude: [/\bcore-js\b/, /\bwebpack\/buildin\b/] }),
      ],
    

    commonjs should be on top of the array.