3

I want to run some TypeScript Code written in ES2020 on Server Side. Currently i have running a ASP.NET Core Application so the idea was to run js via Jering.Javascript.NodeJS and nodejs.

So started with a basic example.

var test = await StaticNodeJSService.InvokeFromFileAsync<string>("Scripts/server/modules/hello.ts", args: new object[] {  });

hello.ts

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

Works...added some import statemant...

hello.ts

import someFunction from "./some.func";

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

--> "Cannot use import statement outside a module"

Nodejs version v16.13.2 and in package.json there is already set

 "type": "module"

changing to require

hello.ts

const someFunction = require("./some.func")

module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = "test"; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}

results in "InvocationException: require() of ES Module ... from ... not supported. Instead change the require of some.func.js in ... to a dynamic import() which is available in all CommonJS modules"

So kind of infinity loop.

By looking here it seems to be possible but how do i get this running?

1
  • See the documentation here take special note of the asterisk on the NodeJS column. Commented Feb 21, 2022 at 11:22

1 Answer 1

2

Found a solution

  1. we have some ES6+ Module which could have itself other import statements.

     let nodeStart = () => {
         return "ES6+ module";
     }
    
     export default nodeStart;
    
  2. use webpack and babel to convert from ES6+ to commonjs

webpack.config.js

const path = require('path');
    const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

    module.exports = (env, options) => {
        console.log(`Webpack 'mode': ${options.mode}`);
        var isDev = options.mode === 'development';
        return {      
            entry: {            
                'some-module-node': "./Scripts/server-test/some-module.ts",
            },
            output: {
                filename: '[name].js',
                path: path.resolve(__dirname, "Scripts/server-test"),
                libraryTarget: 'commonjs'
            },         
            module: {
                rules: [
                    {
                        test: /\.tsx?$/,
                        loader: 'ts-loader',                    
                        exclude: file => (
                            /node_modules/.test(file
                        ))
                    },
                    {
                        test: /\.js$/,
                        exclude: /node_modules/,
                        use: {
                            loader: 'babel-loader',
                            options: {
                                presets: [
                                    ['@babel/preset-env', {
                                        modules: false,
                                        useBuiltIns: 'usage'
                                    }]
                                ]
                            }
                        }                   
                    } 
                ]
            },
            resolve: {
                extensions: [".ts", ".js"]
            },
            target: 'node',      
            devtool: isDev ? "source-map" : undefined,
            plugins: [
                new ForkTsCheckerWebpackPlugin({
                    typescript: {
                       
                    }
                })
            ]        
        }
    };
  1. In packages.json create script for webpack und run it.

  2. "some-module-node.js" will be generated.

  3. use "start-node.js" as entry point for "Jering.Javascript.NodeJS"

    const someModule = require('./some-module-node.js')

     function start(callback, x) {  // Module must export a function that takes a callback as its first parameter
         var result = someModule.default()
    
         callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
     }
    
     module.exports = start
    
  4. from .net core run

      var test = await StaticNodeJSService.InvokeFromFileAsync<string> 
      ("Scripts/server-test/start-node.js", args: new object[] {  });
    

test will be "ES6+ module"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.