Hello,
I would like to know how to run a script during the Next's prebuild stage.
I've installed ts-node as a DevDependency, and added this line in my "scripts" section in the package.json file of my project:
"prebuild": "ts-node ./src/staticAnalysis/runner.ts"
I've created the ./src/staticAnalysis/runner.ts file with some dummy content:
import fs from 'fs';
const filePath = 'dummy/test/file.txt';
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
Now, when I run yarn build, I have the following error:
$ ts-node ./src/staticAnalysis/runner.ts
(node:1091383) 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/stan/delivery/dashboard_rtm/src/staticAnalysis/runner.ts:1
import fs from 'fs';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module.m._compile (/home/stan/delivery/dashboard_rtm/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Object.require.extensions.<computed> [as .ts] (/home/stan/delivery/dashboard_rtm/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Function.Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at phase4 (/home/stan/delivery/dashboard_rtm/node_modules/ts-node/src/bin.ts:649:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I use Next.js 13.