'Npm package working locally but not with from npm repo with npx

I've created a new cli based node application and published it to npm.

When I run the package locally with:

npm run build

which just runs rm -rf lib && tsc -b

and then

npm link
npx my-package arguments

it is working fine.


But when I run directly from npm (not locally and after removing the local one)

npx my-package arguments

It gives me an import error.

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/usr/local/lib/node_modules/my-package/lib/src/cli/lex.js' imported from /usr/local/lib/node_modules/my-package/lib/src/index.js

note sure why?

Problematic code

#!/usr/bin/env node
import { command } from './cli/lex.js';
command.parse(process.argv);

Folder Structure

lib
 - src
 - - index.js
 - - cli
 - - - lex.js
 

Output of ls -l /usr/local/lib/node_modules/my-package/lib/src/cli

-rw-r--r--  1 root  wheel  1893 Oct 26  1985 cli.js.map
-rw-r--r--  1 root  wheel   402 Oct 26  1985 lex.js.map
-rw-r--r--  1 root  wheel  2000 Oct 26  1985 parse.js.map

There are map files but not the actual js

Here is the file in the project (github) if required.



Solution 1:[1]

After hours of debugging and pushing useless path versions if I found out that it was due to this in my .gitignore

*.js

Due to this the js files generated were not being pushed to npm and thus the error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Imanpal Singh