Next.js Discord

Discord Forum

[Prebuild] Running a .ts script, which is importing modules, on the prebuild stage?

Unanswered
Mud-daubers posted this in #help-forum
Open in Discord
Mud-daubersOP
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.

2 Replies

Mud-daubersOP
If I try to append "type": "module" in my package.json file, I get this error:

$ ts-node ./src/staticAnalysis/runner.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/stan/delivery/dashboard_rtm/src/staticAnalysis/runner.ts
    at new NodeError (node:internal/errors:405:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:124:36)
    at defaultLoad (node:internal/modules/esm/load:84:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:603:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I do not know what to do, lol.
Thank you in advance for your replies.
Mud-daubersOP
For now I bypassed it, using const fs = require('fs');, but I don't find this solution very elegant. :/

(And it crashes if I "require" an another TypeScript module using "import"...)
const { DEFAULT_LANGUAGE } = require('../config/i18n');

$ ts-node ./src/staticAnalysis/runner.ts
/home/stan/delivery/dashboard_rtm/src/config/i18n.ts:1
import { getEnumFirstKey } from '@/lib/misc/getEnumKeys';
^^^^^^

SyntaxError: Cannot use import statement outside a module