This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Support ES6 Modules #782
Copy link
Copy link
Closed
Description
I'm trying to package a node app as an exe using pkg, and I'd like to use ES6 imports.
I have something like this in my src/app.js:
import express from 'express'
const app = express()
const eco = (req, res) => {
const { method, url, headers, query } = req
res.json({ method, url, headers, query })
}
app.all('/', eco)
app.listen(3000, () => console.log(`listening on http://localhost:3000`))in my package.json I have:
{
"name": "pkg-test",
"version": "1.0.0",
"description": "",
"main": "src/app.js",
"type": "module",
"type": "module",
"scripts": {
"build": "pkg --targets=node12-win-x64 --output=iisnode-pkg.exe --options experimental-modules src/app.js",
"start": "node --experimental-modules src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"pkg": "^4.4.0"
}
}npm start works fine
$ npm start
> [email protected] start C:\data\devel\apps\tmp\iisnode-pkg
> node --experimental-modules src/app.js
(node:10668) ExperimentalWarning: The ESM module loader is experimental.
welcome to iisnode-pkg
iisnode-pkg listening on http://localhost:3000but npm run build gives a waning and then running the exe throws an error:
>npm run build
> [email protected] build C:\data\devel\apps\tmp\iisnode-pkg
> pkg --targets=node12-win-x64 --output=iisnode-pkg.exe src/app.js --config package.json
> [email protected]
> Warning Failed to make bytecode node12-x64 for file C:\snapshot\iisnode-pkg\src\app.js>iisnode-pkg.exe
C:\snapshot\iisnode-pkg\src\app.js:2
import express from 'express'
^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:701:23)
at Module._compile (pkg/prelude/bootstrap.js:1268:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:768:10)
at Module.load (internal/modules/cjs/loader.js:626:32)
at Function.Module._load (internal/modules/cjs/loader.js:553:12)
at Function.Module.runMain (pkg/prelude/bootstrap.js:1316:12)
at internal/main/run_main_module.js:17:11It seems like the --options experimental-modules parameter from the build script in package.json is not beign taken into account.
Any idea how can I use ES6 module imports from a node app packaged with pkg?
PS: also asked at SO in case anybody wants to raise their reputation
Reacted by Alexander Wunschik, Hakan, Erfan Azary, Lawrence Kesteloot, Rui Engana and Danshil Kokil Mungur
Reacted by Pezhvak and Diego Niquefa
Metadata
Metadata
Assignees
Labels
No labels
Activity
soryy708 commentedon Oct 23, 2019
Make sure you're running a pretty recent version of Node.
The implementation of ESM in Node.js changed in version 12.0.0. "type": "module" wasn't supported in 10.x.
You need to add
optionsto thepkgconfiguration.So, in
package.json:Node documentation: https://nodejs.org/api/esm.html
gt-Lee commentedon Oct 24, 2019
I suggest you package your backend service with webpack before using Pkg
soryy708 commentedon Oct 24, 2019
@gt-Lee Transpilation may not be neccesary with very modern node versions, which support ecmascript modules experimentally. Looks like he's trying to set that up.
[-]problem using ES6 import modules with pkg[/-][+]Suport ES6 Modules[/+][-]Suport ES6 Modules[/-][+]Support ES6 Modules[/+]gautaz commentedon May 12, 2021
Hello,
What is the current status of this issue?
Using
[email protected]with[email protected]still ends up withFailed to make bytecodemessages:The
experimental-modulesflag is not to be used anymore as[email protected]supports ESM natively.Using
webpackseems a bit overkill.What is the best course of action to work around this issue if ESM are not yet supported?
robertsLando commentedon May 12, 2021
@erossignon any clue?
lkesteloot commentedon May 16, 2021
I found a temporary work-around for this: Package everything into a single non-module JavaScript file using webpack. This is my
webpack.config.jsfile:This will start at the root of your source tree (
dist/index.js, for me generated by TypeScript) and generate a singlebinaries/main.jsfile. I can then run:to create the three binaries in the same directory.
gautaz commentedon May 17, 2021
Hello @lkesteloot, I used esbuild with an additional plugin to manage native modules extracted from this esbuild issue.
Still, using an additional bundler seems a bit cumbersome as
pkgis also a bundler of some sort...jesec commentedon May 21, 2021
Check out https://github.com/vercel/ncc . It is quite easy to use.
taylorjdawson commentedon Jun 8, 2021
What would be nice would be for
pkgto a) provide an option to transpile while packaging, b) provide access to the internals (plugin system) so that we could apply a transform to the file before it gets packaged.The root issue here is that tools like babel, swc, and ncc don't follow imports like pkg does. We just need a way to merge the two.
rightaway commentedon Jun 18, 2021
If your target Node version already supports modules then shouldn't
pkgalready work with it? Or doespkgreimplement these features somehow? Can anyone who knows how this works give some info.gautaz commentedon Jun 21, 2021
@rightaway FWIU
pkgtries to find what dependencies should be included in the output by parsing the source code forrequire.To support ES6 modules, it has to do the same for
import.Currently this seems to rely on Babel but I have no insight of the implications of this.
rightaway commentedon Jun 21, 2021
@gautaz If it relies on babel maybe there could be some way to customize the options passed to babel so we can tell if we're using ES6 modules?
@jesec @igorklopov Is it in the eventual roadmap to offer support for ES6 modules or will it not be in the scope of
pkg?ngustavo commentedon Jun 22, 2021
I tried both
"type": "module"and.mjsbut it throwsSyntaxError: Cannot use import statement outside a module. I think they can be added since Node.js is already using them in the stable build. I'm not an expert on how pkg parses dependencies but my help is here if needed.3 remaining items
konsumer commentedon Jul 18, 2021
@vasanthdeveloper how did you run it? I am still getting errors with this:
esbuild --bundle --target=node10 --outdir=./dist --platform=node ./crossaudio.js && pkg --public --out-path dist/ ./dist/crossaudio.jsWhen I try to run the result on mac or linux:
My project is here
Even when I try to run the resulting bundle:
But the source is fine:
If I do this to force cjs extension:
I get same error when I run the build product:
azerpas commentedon Aug 5, 2021
For those using Typescript, set
inside your
tsconfig.jsonryanblock commentedon Sep 24, 2021
Any word on this? ESM has been in the works for years and is now fully supported in active and current Node versions – transpilation should not be a necessary step to using ESM in 2021. This issue in particular has been open for two years.
Related: is this project being actively improved, or is it in bug fix / maintenance mode? Checking in on key issues / offers for PRs have gone unresponded to; I see that occasional maintenance releases are still being occasionally cut, but as far as maintainer participation it seems like crickets here in the issue tracker.
It's totally cool if this project is no longer being actively improved or if PRs just aren't welcomed, but some guidance from the maintainers would be helpful in setting expectations.
leerob commentedon Sep 24, 2021
Feel free to open PRs, we can check them out 😄 As long as there's proper testing, I see no issue adding new features!
gr2m commentedon Sep 24, 2021
@ryanblock please let us know if find a way to make it work :)
Oloompa commentedon Sep 24, 2021
If it can help others, i moved to caxa which handle ESM out-of-the-box.
gr2m commentedon Sep 24, 2021
Worked for me, cheers:
https://github.com/octokit/fixtures-server/blob/3b6d1dfa6d250388fddab9cf3d3ab4fcee3cdb9b/.github/workflows/release.yml
rightaway commentedon Sep 25, 2021
@ryanblock I don't expect much to happen with
pkg.https://github.com/leafac/caxa looks very good. Responsive maintainer and works with ES6 modules and native modules.
ryanblock commentedon Sep 25, 2021
@leerob given the conversation that's ensued, thoughts on the other part of my question (
is this project being actively improved, or is it in bug fix / maintenance mode?)? PRs are welcomed (great!), but are y'all just maintaining, or actively investing in improvingpkg?jesec commentedon Sep 26, 2021
It is straightforward to implement. We just have to support tracing of dependencies for ES6 "import", like what we currently did for CommonJS (see walker.ts).
However, I would rather not reinvent the wheel, since nft does just that. #1138 would be the prerequisite for ES6 module support.
At the moment, I recommend users to use a bundler like esbuild or webpack to transpile to CommonJS style import. If you use TypeScript, simply specify
"module": "commonjs".ryanblock commentedon Sep 26, 2021
Thanks for the context, @jesec! Imo bundling esm to cjs is totally out of the question. Call me crazy, but I like usable stack traces!
Nisthar commentedon Sep 29, 2021
caxa doesn't hide the source code in any way Right? its a big no for me
rightaway commentedon Sep 29, 2021
@Nisthar it will leafac/caxa#21