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:3000
but 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:11
It 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?