importing/require myclass.ts module

Post Reply
pcobee
Member
Posts: 21
Joined: Fri Apr 01, 2011 5:06 pm
Location: Greenville NC
Contact:

importing/require myclass.ts module

Post by pcobee »

I'm creating my first app using TypeScript. I'm trying to import a class that I've created in a seperate .ts file that is at the root level with main.ts. I've tried using both of the following to import the file in main.ts...

import * as myclass from './myclass';
const myclass = require( './myclass ' );

When I activate a flow with my app, I get an error message that my class module can't be found as it's being looked for in the node_modules directory instead of in the root directory.

Cannot find module '<path to my app>\node_modules\myclass' Require stack: - C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\NodeScriptExecutor.js

What is the proper way to include my own modules?
freddyp
Advanced member
Posts: 1022
Joined: Thu Feb 09, 2012 3:53 pm

Re: importing/require myclass.ts module

Post by freddyp »

Is this for an app or for a local script?

If it is for a local script, use the complete path to the file instead of a relative path.

If it is for an app or for another situation in which the usage of a complete path is not possible or desirable, you will have to make sure that your own module ends up in node_modules. The way to do that is to create that module in some other location away from your script. Make sure it is a folder with a package.json file that is correctly configured (npm init) and then in the script folder you do an npm install of the folder with your module.

Note that this takes a snapshot of your own module into the node_modules folder of the script, so if you make changes to the module you have to npm install it again.
crestbrr
Newbie
Posts: 1
Joined: Wed Mar 27, 2024 4:17 pm

Re: importing/require myclass.ts module

Post by crestbrr »

Spent a few days back and forth trying to set configurations in tsconfig.json to fix this. After enough failures, found a very illogical answer to this.

Code: Select all

const myclass = require('./myclass');
should be

Code: Select all

const myclass = require('../myclass');
For whatever reason, NodeScriptExecutor.js defaults every single import path to the node_modules folder. Nothing I've tried in altering the tsconfig.json changes the handling of paths. Changing `./` to `../` effectively allows the program to still use dynamic pathing, while staying functional in its approach.

Still a basic implementation. Seems to work in packed and unpacked environments. Too early to say if this will cause issues down the line, but at least a basic fix to this weird problem.

Looking for some automated solution to set the global path on main.ts, but will keep this thread updated if I find a good way of explicitly stating "hey, these require statements look like they shouldn't work, but this is just how Switch works." Hopefully something like...

Code: Select all

const spc = require('switch-path-correcter')
const myclass = require(spc('./myclass'));
But haven't found anything on npm that does this quite yet. Should be an easy explicit fix once found. For now, I'll just be using the illogical '../myclass' instead of './myclass'

Important caveats:
  • You can only import internal modules using CommonJS syntax of `const foo = require('../foo.js');`. Using `import foo from '../foo.js';` will cause a TS error. It won't throw any transpile errors, but will throw log errors in Switch because it technically didn't transpile correctly.
Post Reply