"build" folder doesn't work alone outside of Node.js project folder

Viewed 27

I have difficulty running my SvelteKit app from a 'build' folder.

When I type npm run build in VS Code, it creates the 'build' output folder. Then when I navigate to the build folder (cd build) and run the app from there (node index.js) the app works.

But if I copy the build folder out of the project's root folder and save it on the desktop, it doesn't work when I run the node index.js command.

Error:

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Isn't a build folder self-contained? Why doesn't it work outside of the project folder?

1 Answers

It turned out that for running app from build folder I need node_module folder and package.json file next to build folder. That's an unfortunate behavior, because I expected that all dependencies are bundled into the build folder, and build folder itself is self-contained. Moreover, I expected treeshaking technique in bundling used modules - only those modules that are used in my code should be included as dependencies in build folder. And because my build folder is my app's starting point, I definetely did not expect that app will look up for dependencies in the parent folder (outside of build folder)

At least they could add that option - to build solution as a self-contained package or only build a core app

Related