0

I would import class in nodejs and use it in app.ts

var nano = require("nano");
import { EnvConfig } from './envConfig.service';
let config = new EnvConfig();
const dbCredentials: any = config.appEnv.getServiceCreds('dataservices');
export const nanodb = nano({
  url: dbCredentials.url,
});
export const nanodbCockpitLight = nanodb.use('data');
console.log(dbCredentials);

When I try to compile I get this error.

import { EnvConfig } from './envConfig.service';
       ^
SyntaxError: Unexpected token {

I have created the tsconfig file :

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist",
    //"baseUrl": "src" // Attention !! nécessite l'utilisation d'un loader de module node pour fonctionner sur node
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

I get this warning

No inputs were found in config file 'c:/Users/EHHD05911.COMMUN/Documents/cockpitLight/DB mananger/tsconfig.json'. Specified 'include' paths were '["src//"]' and 'exclude' paths were '["node_modules","/.spec.ts"]'

5 Answers 5

2

You cannot run node app.ts file directly that won't work You need transpiler like babel js or typescript compiler tsc so first transpile to js file and then run node app.js

Sign up to request clarification or add additional context in comments.

1 Comment

Well, yes and no. Create app.ts with console.log('hello world'); and run node app.ts. "hello world" is printed out. This is because typescript is a superset of javascript and if nothing needs to be transpiled, it works. You can take any .js file, rename it to .ts and so long as it was valid js, it's valid ts. However, once you add typescript syntax (const foo: string = 'bar';) then it breaks.
1

You're using .js extension, you need .ts extension, e.g.: app.ts instead of app.js.

Make sure you have typescript either in npm global or in dev dependencies.

1 Comment

I have added typescript in dev dependencies but still have same problem
1

I suspect whatever you're importing has typescript syntax (strong typing and such), and so running node directly won't work. You need to run tsc first, which will transpile everything to javascript in a dist folder, and then run node dist/app.js.

This is a bit cumbersome though, which is why there is ts-node. It's exactly what it sounds like, a node REPL for typescript. You should be able to run ts-node src/app.ts.

Comments

-1

import { something } is a typescript syntax, it won't work in a .js file. That is a separate language. Try using require instead.

4 Comments

I'm hard coding file name to app.ts,
How are you trying to run it exactly? Please post your command line.
simply node app.ts
This has less to do with typescript/javascript and more with module loading in node. import works for both typescript and es2015+ but you need a transpiler (typescript/babel) in order to change the import statements to require. No need to do this manually.
-1

Use babel js which is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.

package.json

"dependencies": {
"@babel/polyfill": "^7.0.0",
}

"babel": {
"presets": [
  "@babel/preset-env"
]

},

"scripts": {
    "start": "server.js --exec babel-node",
}

https://babeljs.io/docs

This will enable/resolve your import statements.

1 Comment

babel js is a good option but the question is specifically asking about node and typescript, which for this scenario, the typescript compiler (tsc) is the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.