File tree Expand file tree Collapse file tree 5 files changed +108
-3
lines changed
Expand file tree Collapse file tree 5 files changed +108
-3
lines changed Original file line number Diff line number Diff line change @@ -408,6 +408,12 @@ PKG_NODE_PATH=/path/to/node pkg app.js
408408
409409## Troubleshooting
410410
411+ ### Error: Error [ ERR_REQUIRE_ESM] : require() of ES Module
412+
413+ This error is tracked by issue [ #16 ] ( https://github.com/yao-pkg/pkg/issues/16#issuecomment-1945486658 ) . Follow the link in oder to find a workaround.
414+
415+ In most cases adding ` --options experimental-require-module ` to ` pkg ` command line will solve the issue.
416+
411417### Error: Cannot find module XXX (when using ` child_process ` )
412418
413419When using ` child_process ` methods to run a new process pkg by default will invoke it using NodeJS runtime that is built into the executable. This means that if you are trying to spawn the packaged app itself you will get above error. In order to avoid this you must set ` PKG_EXECPATH ` env set to ` "" ` :
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+
3+ 'use strict' ;
4+
5+ const assert = require ( 'assert' ) ;
6+ const utils = require ( '../utils.js' ) ;
7+
8+ // sea is not supported on Node.js < 20
9+ if ( utils . getNodeMajorVersion ( ) < 20 ) {
10+ return ;
11+ }
12+
13+ assert ( __dirname === process . cwd ( ) ) ;
14+
15+ utils . exec . sync ( 'npm install --no-package-lock --no-save' , {
16+ stdio : 'inherit' ,
17+ } ) ;
18+
19+ const input = './test-hybrid.js' ;
20+
21+ const newcomers = [
22+ 'test-hybrid-linux' ,
23+ 'test-hybrid-macos' ,
24+ 'test-hybrid-win.exe' ,
25+ ] ;
26+
27+ const before = utils . filesBefore ( newcomers ) ;
28+
29+ utils . pkg . sync ( [ input , '--options' , 'experimental-require-module' ] , {
30+ stdio : 'inherit' ,
31+ } ) ;
32+
33+ console . log ( 'pkg end' ) ;
34+
35+ try {
36+ console . log ( 'test-hybrid-linux' ) ;
37+ // try to spawn one file based on the platform
38+ if ( process . platform === 'linux' ) {
39+ assert . equal (
40+ utils . spawn . sync ( './test-hybrid-linux' , [ ] ) ,
41+ '8005553535\n' ,
42+ 'Output matches' ,
43+ ) ;
44+ } else if ( process . platform === 'darwin' ) {
45+ assert . equal (
46+ utils . spawn . sync ( './test-hybrid-macos' , [ ] ) ,
47+ '8005553535\n' ,
48+ 'Output matches' ,
49+ ) ;
50+ } else if ( process . platform === 'win32' ) {
51+ assert . equal (
52+ utils . spawn . sync ( './test-hybrid-win.exe' , [ ] ) ,
53+ '8005553535\n' ,
54+ 'Output matches' ,
55+ ) ;
56+ }
57+ } finally {
58+ utils . filesAfter ( before , newcomers ) ;
59+ }
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " test-01-hybrid-esm" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " " ,
5+ "bin" : " test-hybrid.js" ,
6+ "keywords" : [],
7+ "author" : " " ,
8+ "license" : " MIT" ,
9+ "dependencies" : {
10+ "libphonenumber-js" : " ^1.11.20"
11+ },
12+ "pkg" : {
13+ "patches" : {
14+ "./node_modules/libphonenumber-js/package.json" : [
15+ " \" type\" : \" module\" ," ,
16+ " "
17+ ]
18+ }
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const { findPhoneNumbersInText } = require ( 'libphonenumber-js' ) ;
4+
5+ const res = findPhoneNumbersInText (
6+ `
7+ For tech support call +7 (800) 555-35-35 internationally
8+ or reach a local US branch at (213) 373-4253 ext. 1234.
9+ ` ,
10+ 'US' ,
11+ ) ;
12+
13+ console . log ( res [ 0 ] . number . nationalNumber ) ;
Original file line number Diff line number Diff line change @@ -150,6 +150,7 @@ module.exports.pkg = function () {
150150} ;
151151
152152const es5path = path . resolve ( __dirname , '../lib-es5/bin.js' ) ;
153+ const tsPath = path . resolve ( __dirname , '../lib/bin.ts' ) ;
153154
154155/**
155156 *
@@ -159,9 +160,15 @@ const es5path = path.resolve(__dirname, '../lib-es5/bin.js');
159160 */
160161module . exports . pkg . sync = function ( args , opts ) {
161162 args = args . slice ( ) ;
162- const es5 = existsSync ( es5path ) ;
163- args . unshift ( es5path ) ;
164- assert ( es5 , 'Run `yarn build` first!' ) ;
163+
164+ if ( process . env . DEV === 'true' ) {
165+ args . unshift ( tsPath ) ;
166+ args . unshift ( '-r' , 'esbuild-register' ) ;
167+ } else {
168+ const es5 = existsSync ( es5path ) ;
169+ args . unshift ( es5path ) ;
170+ assert ( es5 , 'Run `yarn build` first!' ) ;
171+ }
165172
166173 if ( Array . isArray ( opts ) ) opts = { stdio : opts } ;
167174
You can’t perform that action at this time.
0 commit comments