How can I run a script at next dev?

Issue

This Content is from Stack Overflow. Question asked by Dimitri Borgers

I have a NextJS project where I need to run a file after running npm run dev in dev or npm run start in prod. To do this, I’ve tried modifying my package.json:

"scripts": {
    "dev": "next dev && ts-node --compiler-options {"module":"CommonJS"} services/airdropLoop.ts",
    "build": "next build",
    "start": "next start && ts-node --compiler-options {"module":"CommonJS"",
    "postinstall": "prisma generate"
  },

Note that the reason I use --compiler-options "{"module":"CommonJS"}" is because I use ES6 import syntax, not commonjs syntax (i.e. require()). However, if I do this, I keep getting the error:

> dev
> ts-node --compiler-options "{"module":"CommonJS"}" services/airdropLoop.ts

undefined:1
{module:CommonJS}
 ^

SyntaxError: Unexpected token m in JSON at position 1
    at JSON.parse (<anonymous>)
    at parse (/Users/dimitriborgers/Documents/3mint/3mint-core/node_modules/ts-node/dist/util.js:52:45)
    at arg (/Users/dimitriborgers/Documents/3mint/3mint-core/node_modules/ts-node/node_modules/arg/index.js:122:24)
    at parseArgv (/Users/dimitriborgers/Documents/3mint/3mint-core/node_modules/ts-node/dist/bin.js:69:12)
    at main (/Users/dimitriborgers/Documents/3mint/3mint-core/node_modules/ts-node/dist/bin.js:25:18)
    at Object.<anonymous> (/Users/dimitriborgers/Documents/3mint/3mint-core/node_modules/ts-node/dist/bin.js:579:5)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

How can I run my script? Is this not the right way to immediately run a file after dev or start?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?