wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SyntaxError: Cannot use import statement outside a module

hp8wvvvgnj6asjm7 opened this issue · comments

I really went trough all the duplicates of this question, and no solution has worked. This is something different.

My project works perfectly on my local machine, but as soon as I run it on a server, I get this error:

SyntaxError: Cannot use import statement outside a module.

Nothing is run in the browser.

It is just server side ts

/root/ae-system/node_modules/rithmic-api/src/index.ts:1
import os from 'os'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:74:18)
    at wrapSafe (node:internal/modules/cjs/loader:1141:20)
    at Module._compile (node:internal/modules/cjs/loader:1182:27)
    at Module._compile (/usr/lib/node_modules/ts-node-dev/node_modules/source-map-support/source-map-support.js:568:25)
    at Module.m._compile (/tmp/ts-node-dev-hook-1410233924574671.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at require.extensions..jsx.require.extensions..js (/tmp/ts-node-dev-hook-1410233924574671.js:114:20)
    at require.extensions.<computed> (/tmp/ts-node-dev-hook-1410233924574671.js:71:20)
    at Object.nodeDevHook [as .ts] (/usr/lib/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
[ERROR] 13:49:17 SyntaxError: Cannot use import statement outside a module

The error above occurs at a local npm module. The module was not installed from the npm registry but is a custom typescript project.

The dependency was added at the main folder:

package.json

...
  "dependencies": {
    "rithmic-api": "file:rithmic-api",
  }
...

I don't know if it is caused because of this.

My local npm and node versions are almost equal to the server versions:

node is > 19
npm is > 8

As for the Typescript configurations:

the custom npm module has

tsconfig.json

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "CommonJS",
        "outDir": ".dist/src",
        "moduleResolution": "node",
        "listFiles": false,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noFallthroughCasesInSwitch": true,
        "forceConsistentCasingInFileNames": true,
        "pretty": true,
        "sourceMap": false,
        "types": ["node"],
        "esModuleInterop": true,
        // "declaration": true,
        // "rootDir": "sys",
        // "skipLibCheck": true,
        // "noStrictGenericChecks": true,
        // "noUnusedParameters": false,
        // "experimentalDecorators": true,
        // "emitDecoratorMetadata": true,
    },
    "include": ["./src/**/*.ts"],
    "exclude": ["node_modules"],
}

the main project folder has

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "CommonJS",
        "outDir": ".dist/src",
        "moduleResolution": "node",
        "listFiles": false,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noFallthroughCasesInSwitch": true,
        "forceConsistentCasingInFileNames": true,
        "pretty": true,
        "sourceMap": false,
        "types": ["node"],
        "esModuleInterop": true,
        // "declaration": true,
        // "rootDir": "sys",
        // "skipLibCheck": true,
        // "noStrictGenericChecks": true,
        // "noUnusedParameters": false,
        // "experimentalDecorators": true,
        // "emitDecoratorMetadata": true,
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"],
}

The main scripts to run the project are:

"scripts": {
    "start": "ts-node ./src/index.ts",
    "dev": "tsnd --cache-directory \".cache\" --watch \"./rithmic-api/src, ./strategy\" --respawn --clear --no-warnings ./src/index.ts",
    "build": "tsc"
  },