Reputation: 5235
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"]'
Upvotes: 0
Views: 3297
Reputation: 2331
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
.
Upvotes: 1
Reputation: 265
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
Upvotes: 2
Reputation: 1057
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",
}
This will enable/resolve your import statements.
Upvotes: -1
Reputation: 2022
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.
Upvotes: 1
Reputation: 853
import { something }
is a typescript syntax, it won't work in a .js
file. That is a separate language. Try using require
instead.
Upvotes: -1