0

I am in a Svelte project and I have an issue:

  • My files are showing no errors in VSCode, but when executing npm run dev -- all Typescript syntax are shown as error and the server won't start.

So I tried removing all node_modules and reinstalling them, but now when I run node scripts/setupTypeScript.js I have the following error:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\(...)\my-app\scripts\setupTypeScript.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

I tried reinstalling TypeScript globally (npm install typescript@latest -g), but that didn't help. I didn't find anyone with the same issue...

-- Note that it was working perfectly fine, until I did something I don't recall (trying to write something but it was received as shortcut and tampered with my files - I couldn't find anything special in the diff from the git repo, except package.json some version (TS related) were upgraded.)

Edit

(Before to reproduce my mistakes, please look below at the answer.)

So apparently the ./scripts/ folder should exists and doesn't. So i recreated it :

mkdir tmp
cd tmp
npx degit sveltejs/template svelte-typescript-app
cp -r svelte-typescript-app/scripts ../
cd ../
rm -r tmp

But running it now gives me another error that I didn't have before:

> node scripts/setupTypeScript.js
node:internal/fs/utils:344
    throw err;
    ^

    at Object.renameSync (node:fs:980:3)
    at Object.<anonymous> (C:\(...)\my-app\scripts\setupTypeScript.js:44:4)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153: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:81:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -4058,
  syscall: 'rename',
  code: 'ENOENT',
  path: 'C:\(...)\my-app\src\\main.js',
  dest: 'C:\(...)\my-app\src\\main.ts'
}

Actually it is trying to convert main.js into main.ts, but because I already run it, it doesn't work. So I renamed it back into main.js and it was successful, but now running npm run dev -- gives me an error about Error: Identifier 'sveltePreprocess' has already been declared, indeed, it appears twice in rollup.config.js, just removing the redundancy solves the issue. Warning: There are other duplicated lines.

Conclusion

In the end I still have the syntax error popping up, and the scripts/ directory disappeared again. See the answer below

vinalti
  • 966
  • 5
  • 26
  • Does `scripts/setupTypeScript.js` exist? The error says it doesn't. – Slava Knyazev Jul 01 '22 at 20:49
  • The folder doesn't exist but as far as I know the folder didn't exist before and it was working still. Could it be somewhere else ? – vinalti Jul 02 '22 at 11:53
  • So Executing this script a second time was not the solution, and was more a bad idea. It didn't solve my original issue, so I made another post about it, if you want to help : https://stackoverflow.com/q/72841684/17965313 – vinalti Jul 02 '22 at 18:58

2 Answers2

0

it looks as if this path (scripts/setupTypeScript.ts) doesn't exist. Are you running the node command in the folder that contain the scripts folder?

  • The folder doesn't exist but as far as I know the folder didn't exist before and it was working still. It seems to be part of svelte, could it be somewhere else (in the system Node's directory or so) ? – vinalti Jul 02 '22 at 11:43
0

So, for those in the same situation, here is the detailled explanation:

  • The setupTypeScript.js is not working because the directory scripts/ is missing.
  • If you already ran it, the script automatically deleted itself, and its folder. (reason why it's missing...)
  • Fact is, that you should not re-run the script setupTypeScript.js a second time, if you already ran it.
  • If you are not sure if you already ran it, check the file main.js/ts if it is named : main.ts the script setupTypeScript.js has probably already been run.
  • If you think you didn't execute the script setupTypeScript.js and you need to do it but don't have it, then you can download it like this
npx degit sveltejs/template/scripts scripts

If your main is already named main.ts, it will throw an error (at Object.renameSync (node:fs:980:3)) then, rename it to main.js.
If you already ran the script, it will duplicate some lines in rollup.config.js, make sure : import sveltePreprocess from 'svelte-preprocess'; and import typescript from '@rollup/plugin-typescript'; are not duplicated.

Hope it helps.

vinalti
  • 966
  • 5
  • 26