Cannot find module when using with ES Modules #1291
Comments
|
You didn't post your complete package.json file. So i think you didn't add your sub folders as scripts. For example if you want to add dhjaks folder as script folder then you need to add this in package file like. |
|
@sartaj-singh I actually did post my complete The I did it like this to make a minimal test case that shows the problem. I now tried to use $ mkdir foobar
$ cd foobar
$ echo '{ "name": "test", "bin": "index.js", "type": "module" }' > package.json
$ echo 'import os from "os"' > index.js
$ echo 'console.log(os.arch())' >> index.js
$ npx pkg package.json
> pkg@5.3.1
> Warning Failed to make bytecode node16-arm64 for file /snapshot/foobar/index.js
$ ./test
node:internal/modules/cjs/loader:930
throw err;
^
Error: Cannot find module '/snapshot/foobar/index.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
at Function._resolveFilename (pkg/prelude/bootstrap.js:1776:46)
at Function.Module._load (node:internal/modules/cjs/loader:772:27)
at Function.runMain (pkg/prelude/bootstrap.js:1804:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
} |
|
Check this line:- Warning Failed to make bytecode node16-arm64 for file /snapshot/foobar/index.js |
Setting the targets doesn't change anything, I've tried different targets and even running on different platforms... It does however run if I don't use |
|
I always create const with require. import statement may be not supported by pkg. I think no need type=module if you compile stand alone executable. |
If it isn't, then this is a feature request
I need it because I need to import packages which are ESM-only |
|
Hi, could someone clarify clearly on the home page (README.md) whether or not pkg supports ESMs (ES modules) at all? I know it's a free open-source labor-of-love project so I am not demanding anything. It is what it is and it is appreciated as-is. Just would like a clear positioning so we don't need to waste our time trying to package "type": "module" projects, if that's not supported at all. ESMs are not exactly a new invention so a one-liner positioning in the docs would be helpful. If ESM packaging is hopeless with pkg, does anyone know of a workaround (other than rewriting all your code back into CommonJS)? Cheers! |
|
There is the option of using a barebone webpack config to create a single JS file containing all dependencies and not having any external import. Something like this: The output is then usable with It should also be possible to update |
|
For anyone interested I suggest you to firstly use ncc to compile your modules and then use pkg to compile them into executable. There is already an open feature request to include ncc in pkg, maybe with an option |
|
That was what we were doing until a recent update of |
Cannot this be disabled with an option? |
|
Not with an option, sadly. But in the end, |
|
By double checking the code seems import statements should be supported: Line 258 in 59125d1 Maybe something isn't working as expected |
|
I tried to look into this but haven't find the root cause, the build process seems to work as the import statement is recognized correctly but then the produced binary isn't working 🤷🏼♂️ |
|
The exact issue, on a very minimalist project:
It will output this: And the binaries are unusable: Removing as expected. And since which I traced back to the babel config, and prevent the binary from being build. Quick-fixing this config issue brings us back to the issues described above without using |
Would you happen to know what specific version of ncc made this change? I am also facing the issue addressed in this issue and am wondering if we could not just downgrade to an ncc version prior to that change and use that? |
|
The change was introduced in with ncc@0.29.0. Since we stopped using it I can't tell if something changed in later releases though. |
I'm getting the following error as soon as the compiled app boots:
Here is a minimal reproducible example:
package.json{ "type": "module" }index.jsBuild command:
The text was updated successfully, but these errors were encountered: