1

I've created npm package

package.json:

{
  "name": "playwrightlib",
  "version": "0.0.7",
  "description": "Lib with playwright dependency",
  "repository": "git@github.com:DonTomato/playwrightlib.git",
  "author": "myemail@gmail.com",
  "license": "MIT",
  "type": "module",
  "exports": "./src/index.js",
  "files": [
    "src",
    "*.md"
  ],
  "engines": {
    "node": "^16.15.0"
  },
  "dependencies": {
    "jest": "^29.5.0",
    "@playwright/test": "^1.28.1"
  }
}

It contains src folder with two files:

index.js:

export { testAbout } from './actions';

And actions.js:

import { expect } from '@playwright/test';

/**
 * test About page
 * @param {object} page - current page
 */
export async function testAbout(page) {
  await page.goto('/about');
  await expect(page.getByRole('heading', { name: 'About this app' })).toBeVisible();
}

When I try to use it with import { testAbout } from 'playwrightlib'; it returns the following error:

Error: Cannot find module 'C:...\node_modules\playwrightlib\src\actions' imported from C:...\node_modules\playwrightlib\src\index.js

Why? And how to fix it?

Don Tomato
  • 3,311
  • 3
  • 30
  • 48
  • 1
    It's `export { testAbout } from './actions.js';`. Add the file extension. – jabaa Apr 13 '23 at 08:50
  • Yes, thanks! Could you create an answer, and will mark it as solved – Don Tomato Apr 13 '23 at 09:28
  • 1
    That's a typo. It's a close reason. I've voted to close as typo. The problem should be pretty obvious with the error message `Error: Cannot find module 'C:...\node_modules\playwrightlib\src\actions'`. You can also close as duplicate: [Does ES6 import/export need ".js" extension?](https://stackoverflow.com/questions/44481851/does-es6-import-export-need-js-extension) – jabaa Apr 13 '23 at 09:43

0 Answers0