node-fs triggers build error with spago/esbuild
flip111 opened this issue · 3 comments
I am not sure whether this is a node-fs or spago problem, therefor i'm cross posting. Please close when this issue is not relevant for this repo.
Reproduce & error:
mkdir nodefssync
cd nodefssync
spago init
spago install node-fs node-buffernow paste source code into Main.purs
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
import Node.FS.Sync
import Node.Encoding
main :: Effect Unit
main = do
_ <- readTextFile UTF8 "blabla"
log "🍝"spago bundle-app --platform=node
node index.js» node index.js
(node:654122) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/flip111/nodefssync/index.js:109
import {
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:18:47
Node.js v18.8.0
Related file: https://github.com/purescript-node/purescript-node-fs/blob/c6c52c510b55940bbcf475fa25913bc7eb415acb/src/Node/FS/Sync.js
node-fs post: purescript-node/purescript-node-fs#68
I don't find this too surprising, it's a consequence of the PS ecosystem moving to ES modules.
The immediate fix is basically to do as it says - either:
- add
"type": "module"in thepackage.json
or
- bundle to a file with a
.mjsextension
One thing that might be nice to have is to have a platform option for bundle-app that corresponds with the esbuild argument of the same name. Or maybe a esbuild-args option that works like purs-args, since I guess there's also target and external options that might be useful to provide.
Just out of curiosity, why bundle at all if you're making a node app?
It's my first nodejs development. I thought i was supposed to compile it to a single file, for no other reason than i was doing that for frontend and i'm used to that for compiled languages. Other than that it seems cool that if you add a shebang to the file you can put it into a bin folder as if it's a binary.
spago@next now supports this - if you run the example from the first post with the latest (0.93.14), this is the error that it returns (as one would expect):
$ node index.js
node:fs:603
handleErrorFromBinding(ctx);
^
Error: ENOENT: no such file or directory, open 'blabla'
at Object.openSync (node:fs:603:3)
at readFileSync (node:fs:471:35)
at file:///Users/fabrizio/code/spago/nodefssync/index.js:364:14
at __do (file:///Users/fabrizio/code/spago/nodefssync/index.js:373:37)
at file:///Users/fabrizio/code/spago/nodefssync/index.js:378:1
at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: 'blabla'
}
Node.js v18.17.1Closing as fixed.