ESM module support? #145
laurencefass
started this conversation in
General
Replies: 1 comment 1 reply
-
Not yet, there is #16 to track updates but there are workarounds |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been trying to pkg a Next JS module and getting one error after another. Seems to be related to ESM modules.
Am I correct in reaching the assumption that pkg wont support handling of esm modules with imports and mjs files.
Ive tried setting up a really simple express server which reinforces this idea.
package.json
{
"name": "hello-exe",
"version": "1.0.0",
"description": "A simple hello world app packaged with pkg",
"main": "hello.mjs",
"scripts": {
"build:pkg": "pkg hello.mjs --output hello-exe --targets node20-win-x64 --force --debug"
},
"pkg": {
"scripts": [
"hello.mjs"
]
},
"dependencies": {
"express": "^4.17.1"
}
}
hello.mjs (using import)
import express from 'express';
const app = express();
const port = 3000;
console.log('pkg generated executable is working');
app.get('/', (req, res) => {
console.log("hello.js / endpoint hit");
res.send('Hello from Express! pkg generated executable is working');
});
app.get('/test', (req, res) => {
console.log("hello.js /test endpoint hit");
res.send('Testing 1... 2... 3...');
});
app.listen(port, () => {
console.log(
Server running at http://localhost:${port});});
result of running npm build:pkg
C:\snapshot\FoTL_CAM\projects\pkg-test\hello.mjs:1
(function (exports, require, module, __filename, __dirname) { import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at new Script (node:vm:118:7)
at Socket. ([eval]:18:19)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:191:23)
....this works fine if i use cjs format. this produces the correct output
const express = require('express');
const app = express();
const port = 3000;
console.log('pkg generated executable is working');
app.get('/', (req, res) => {
console.log("hello.js / endpoint hit");
res.send('Hello from Express! pkg generated executable is working');
});
app.get('/test', (req, res) => {
console.log("hello.js /test endpoint hit");
res.send('Testing 1... 2... 3...');
});
app.listen(port, () => {
console.log(
Server running at http://localhost:${port});});
Beta Was this translation helpful? Give feedback.
All reactions