Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Any future support for ES6 Node application #439

Closed
shahajipatil opened this issue Jun 5, 2018 · 18 comments
Closed

Any future support for ES6 Node application #439

shahajipatil opened this issue Jun 5, 2018 · 18 comments

Comments

@shahajipatil
Copy link

Any future updates for supporting ES6 node application. My application is developed in ES6 compatible code like import which is not supported by PKG.

@sheepsteak
Copy link

pkg is just using Node and there is information here about using import in Node - https://nodejs.org/api/esm.html

To turn that on in pkg you'll need to see here - https://github.com/zeit/pkg#options

You'd do something like this: pkg app.js --options experimental-modules

@theogravity
Copy link

You can also use babel to transpile your code and package that instead.

@zoutepopcorn
Copy link

@theogravity did you try to use pkg on a transpiled babel project?

My node transpiled.js worked fine, after building...

But then I got the same error (outup as warning):
#211

> Warning Failed to make bytecode node8-x64 for file /snapshot/shadertoy/src/main.js
> Warning Failed to make bytecode node8-x64 for file /snapshot/shadertoy/src/ui.js
> Warning Failed to make bytecode node8-x64 for file /snapshot/shadertoy/src/Effect.js

I was just wondering if anyone is able to pkg for linux, windows and mac.
I'm trying to make it work on Arch Linux

@zoutepopcorn
Copy link

@shahajipatil
Ok now this example is working for me:
https://github.com/zoutepopcorn/bapkg

@OmgImAlexis
Copy link

Would love to see support for esm especially as a lot of my dependencies are using it.

@FranklinYu
Copy link

FranklinYu commented Feb 14, 2019

I got this error when I use import.meta:

Error! This experimental syntax requires enabling the parser plugin: 'importMeta'

How do I enable a plugin? Parser plugin isn't mentioned in README.

Note that I'm using Node.js 10, which supports import.meta.url. In addition, Babel is not yet ready for import.meta.url, so I cannot simply transpile and make it work.

@ChrisMagnuson
Copy link

@sheepsteak's suggestion works as long as there are no imports but something as simple as the following fails:

import os from "os"
console.log(os.hostname())

Saving this to a file called TestESMPKG.js and running pkg .\TestESMPKG.js --options experimental-modules --targets latest-win-x64 --debug results in the following errors:

SyntaxError: Unexpected identifier
    at new Script (vm.js:84:7)
    at Socket.<anonymous> ([eval]:18:19)
    at Socket.emit (events.js:196:13)
    at addChunk (_stream_readable.js:290:12)
    at readableAddChunk (_stream_readable.js:271:11)
    at Socket.Readable.push (_stream_readable.js:226:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:166:17)
(node:19736) ExperimentalWarning: The ESM module loader is experimental.
C:\snapshot\TestESMPKG\TestESMPKG.js:1
(function (exports, require, module, __filename, __dirname) { import os from "os"
                                                                     ^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:84:7)
    at Socket.<anonymous> ([eval]:18:19)
    at Socket.emit (events.js:196:13)
    at addChunk (_stream_readable.js:290:12)
    at readableAddChunk (_stream_readable.js:271:11)
    at Socket.Readable.push (_stream_readable.js:226:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:166:17)
> Warning Failed to make bytecode latest-x64 for file C:\snapshot\TestESMPKG\TestESMPKG.js

@OmgImAlexis
Copy link

@ChrisMagnuson wouldn't you need to use .mjs since you're using the experimental-modules flag?

@gigouni
Copy link

gigouni commented Feb 18, 2020

Did some of you succeeded to use pkg without transpiling? @OmgImAlexis @ChrisMagnuson

@gmcdev
Copy link

gmcdev commented Jun 2, 2020

I gave up and webpacked my node backend -- got it packaged. Still struggling with __dirname pathing to runtime assets, as __dirname is no more with type="module".

@TomasHubelbauer
Copy link

@gmcdev __dirname can be obtained from module.import in ESM modules, but unfortunately, it looks like that doesn't work either:

Error! import.meta may appear only with 'sourceType: "module"' (54:22)

It is the same with --options require=ems and --options experimental-modules. I notice @FranklinYu has posted about the same problem some time ago already so I don't think this will get anytime soon, unfortunately.

@Akiyamka
Copy link

Akiyamka commented Dec 2, 2020

Still fail

pkg index.mjs --targets node14-linux-x64

@leerob
Copy link
Member

leerob commented Mar 1, 2021

Support will be for Node 12 and 14 going forward. Thanks!

https://endoflife.date/nodejs

@rightaway
Copy link

@leerob So ES6 modules should already work with pkg with Node 14 or it's still in development?

@jim-king-2000
Copy link

I try it with pkg@5.4.1 and node@16.13. Command line pkg .\app.mjs -t node16-win-x64 can generate app.exe on win10. But when I run it, we see the error message:

PS C:\Users\Jim_K\Downloads\testpkg> ./app
node:internal/modules/cjs/loader:979
    throw new ERR_REQUIRE_ESM(filename, true);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\snapshot\testpkg\app.mjs not supported.
Instead change the require of C:\snapshot\testpkg\app.mjs to a dynamic import() which is available in all CommonJS modules.
    at Function.runMain (pkg/prelude/bootstrap.js:1847:12) {
  code: 'ERR_REQUIRE_ESM'
}

@rightaway
Copy link

@leerob So ES6 modules should already work with pkg with Node 14 or it's still in development?

@HolisticPeanut
Copy link

I find it weird that even though node 16 is in Active LTS, neither pkg or nexe work with modules and nothing seems to move in this direction.

I have moved my project to modules because some npm packages are module only now. It looks like at this point in time I either have to use old packages or can't compile my project (sad)

https://nodejs.org/en/about/releases/

@robertsLando
Copy link
Contributor

@GeneralBaduar Follow updates #1323

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests