Skip to content

Expose more APIs #7992

@lukemovement

Description

@lukemovement

I have recently been trying to build custom layers, however, as I am using ESM I am unable to access a large number of things from the library. For example, including the imports below will prevent the .mts file from being transpiled by ts-node-esm. Would it be possible to expose these methods so that they can be used? (in the same way tf.layers exposes various layers for example)

import { getConstraint, serializeConstraint } from '../constraints';
import {ValueError} from '../errors';
import {getInitializer, serializeInitializer} from '../initializers';
import {getRegularizer, serializeRegularizer} from '../regularizers';
import {getExactlyOneShape, getExactlyOneTensor} from '../utils/types_utils';
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["ESNext", "DOM"],
    "types": ["node"],
    "moduleResolution": "Node",
    "outDir": "dist",
    "strict": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "verbatimModuleSyntax": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "alwaysStrict": true,
    "allowJs": true
  },
  "include": ["src", "tests"]
}

"type": "module", is present within package.json
yarn ts-node-esm path/to/file.mts is used to execute the script


import tf from "@tensorflow/tfjs-layers/dist/constraints.js";
const { getConstraint } = tf;
console.log(getConstraint);
(node:40133) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/node_modules/@tensorflow/tfjs-layers/dist/constraints.js:11
import * as tfc from '@tensorflow/tfjs-core';
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .js] (/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

import tf from "@tensorflow/tfjs-layers/dist/constraints";
const { getConstraint } = tf;
console.log(getConstraint);
/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366
    throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: Cannot find module '/node_modules/@tensorflow/tfjs-layers/dist/constraints' imported from /src/test.mts
    at finalizeResolution (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
    at moduleResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
    at Object.defaultResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/node_modules/ts-node/src/esm.ts:168:34)
    at /node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/node_modules/ts-node/src/esm.ts:197:12)
    at resolve (/node_modules/ts-node/src/child/child-loader.ts:15:39)
    at nextResolve (node:internal/modules/esm/loader:163:28)

import * as tf from "@tensorflow/tfjs-layers/dist/constraints";
const { getConstraint } = tf;
console.log(getConstraint);
/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366
    throw new ERR_MODULE_NOT_FOUND(
  ^
CustomError: Cannot find module '/node_modules/@tensorflow/tfjs-layers/dist/constraints' imported from /src/test.mts
    at finalizeResolution (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
    at moduleResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
    at Object.defaultResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/node_modules/ts-node/src/esm.ts:168:34)
    at /node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/node_modules/ts-node/src/esm.ts:197:12)
    at resolve (/node_modules/ts-node/src/child/child-loader.ts:15:39)
    at nextResolve (node:internal/modules/esm/loader:163:28)

import * as tf from "@tensorflow/tfjs-layers/dist/constraints.js";
const { getConstraint } = tf;
console.log(getConstraint);
(node:40396) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/node_modules/@tensorflow/tfjs-layers/dist/constraints.js:11
import * as tfc from '@tensorflow/tfjs-core';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .js] (/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

import { getConstraint } from "@tensorflow/tfjs-layers/dist/constraints";
console.log(getConstraint);
/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366
    throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: Cannot find module '/node_modules/@tensorflow/tfjs-layers/dist/constraints' imported from /src/test.mts
    at finalizeResolution (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
    at moduleResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
    at Object.defaultResolve (/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/node_modules/ts-node/src/esm.ts:168:34)
    at /node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/node_modules/ts-node/src/esm.ts:197:12)
    at resolve (/node_modules/ts-node/src/child/child-loader.ts:15:39)
    at nextResolve (node:internal/modules/esm/loader:163:28)

import { getConstraint } from "@tensorflow/tfjs-layers/dist/constraints.js";
console.log(getConstraint);

file:///src/test.mts:87
import { getConstraint } from "@tensorflow/tfjs-layers/dist/constraints.js";
         ^^^^^^^^^^^^^
SyntaxError: Named export 'getConstraint' not found. The requested module '@tensorflow/tfjs-layers/dist/constraints.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@tensorflow/tfjs-layers/dist/constraints.js';
const { getConstraint } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Activity

self-assigned this
on Oct 9, 2023
gaikwadrahul8

gaikwadrahul8 commented on Oct 9, 2023

@gaikwadrahul8
Contributor

Hi, @lukemovement

Thank you for bringing this issue to our attention. Could you please let us know if you are receiving any error messages when transpiling with ts-node-esm while trying to build custom layers? If so, please share the error log with us with code-snippet or your Github repo link with steps to replicate the same behaviour from our end which will help us to investigate this issue further from our end. Thank You.

lukemovement

lukemovement commented on Oct 10, 2023

@lukemovement
Author

Hi, @lukemovement

Thank you for bringing this issue to our attention. Could you please let us know if you are receiving any error messages when transpiling with ts-node-esm while trying to build custom layers? If so, please share the error log with us with code-snippet or your Github repo link with steps to replicate the same behaviour from our end which will help us to investigate this issue further from our end. Thank You.

I've updated the OP with the error logs

gaikwadrahul8

gaikwadrahul8 commented on Oct 17, 2023

@gaikwadrahul8
Contributor

Hi, @lukemovement

I apologize for the delayed response and I tried to replicate the same issue from my end and I'm getting the below error message after adding "type": "commonjs", "type": "module" in package.json file appropriately so we'll have to dig more into this issue and will update you soon.

Thank you for noticing this behaviour while using ESM. Thank you!

gaikwadrahul-macbookpro:test-7992 gaikwadrahul$ node index.js
(node:52564) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/gaikwadrahul/Desktop/TFJS/test-7992/node_modules/@tensorflow/tfjs-layers/dist/constraints.js:11
import * as tfc from '@tensorflow/tfjs-core';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.17.0
gaikwadrahul-macbookpro:test-7992 gaikwadrahul$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @pyu10055@lukemovement@gaikwadrahul8

      Issue actions

        Expose more APIs · Issue #7992 · tensorflow/tfjs