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: > ts-node ./src/index.ts
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"
},
CodePudding user response:
The issue was caused by myself or some change in npm where local packages are not linked, but installed
I used these instructions to fix the issue:
https://www.geeksforgeeks.org/how-to-install-a-local-module-using-npm/
CodePudding user response:
This looks like a problem of typescript not finding your tsconfig.json
file. Your config looks fine. Something that I am wondering is why don't you build everything into the dist
folder and just deploy the dist
folder to your server this way the modules are build into the dist
? I think node
running javascript
is faster that ts-node
running typescript
.
Fixed by OP after discussion below:
i fixed the problem using these instructions. geeksforgeeks.org/how-to-install-a-local-module-using-npm