Next.js Discord

Discord Forum

Run a separated typescript file in a nextjs project

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
Hey i'm beign mad trying to run a .ts file in a separated process into a nextjs project.
I've a monitor.ts file that should be runned with a pm2 process, i can run it with tsx monitor.ts but seems that i can't use tsx with pm2, then i tried with ts-node but with ts-node seems that i can import the libs from node_modules, but i can't import my funcions exported from other files.
that's my tsconfig:

{
  "compilerOptions": {
    "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "module": "ESNext", /* Specify what module code is generated. */ // "rootDir": "./",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "node",
    "baseUrl": "./", 
    "paths": {
      "@/*": [
        "./*"
      ],
      "components/*": [
        "./components/*"
      ],
      "utils/*": [
        "./utils/*"
      ]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true, 
    "plugins": [
      {
        "name": "next"
      }
    ],
    "allowJs": true,
    "noEmit": true,
    "incremental": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "next-env.d.ts",
    ".next/types/**/*.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
  "ts-node": {
    "esm": true,
    "compilerOptions": {
      "module": "CommonJS"
    }
  }
}

the error after doing: npx ts-node monitor.ts
ReferenceError: exports is not defined in ES module scope
I'm not doing any exports and if i remove the Commonjson option i get Cannot find package '@/utils
seems that it is not able to resolve path, but in next the same imports works

0 Replies