Skip to content

Expose more APIs #7992

Open
Open
@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)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions