question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Possible to import ts modules from node_modules?

See original GitHub issue

Hi,

We are, at my company, publishing a lot of private typescript npm modules. It is really handy to not compile the typescript files before publishing the module and letting the consumer transpile the code after its need. With tools like webpack, rollup, vite, esbuild this is really easy and has a lot of benefits.

However, when we now want to use a typescript file that is placed in node_modules in a Playwright test we got an error, probably because Playwright does not compile the code inside node_modules. Is this possible to configure?

Example:

// node_modules/my-package/index.ts
export const myNumber: number = 5;

// tests/example.spec.ts
import { test } from "@playwright/test";
import { myNumber } from "my-package";

test("My test", () => {
  console.log(myNumber);
});

Gives the following error when running npx playwright test:

node_modules/my-package/index.ts:1

export const myNumber: number = 5;
^^^^^^

SyntaxError: Unexpected token 'export'

    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)

So is this possible to configure or do we have to pre-compile our module?

Thanks

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
oskarkivracommented, Nov 11, 2022

@lesha1201 I agree; there is plenty of use cases, and node_modules do not only contain javascript code these days.

I can reopen this and let @pavelfeldman and the rest of the team decide if this should have more attention or not.

1reaction
RickMeijercommented, Nov 11, 2022

Thank you! We are working in a monorepo, and have some shared (private) packages that are typescript only, for convenience sake (to add another use case).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with import from node_modules in javascript
js - you just need to write: import helloWorld from './app.js';. and it's working! As for node modules, this is more complicated ...
Read more >
Getting started with Node.js modules: require, exports, imports ...
Getting started with Node.js modules: require , exports , imports , and beyond. Modules are a crucial concept to understand Node.js projects ...
Read more >
Common TypeScript module problems and how to solve them
Problem 1: Irregular location of dependencies ; node_modules in the ; src directory even though ; node_modules is located outside the ; src ......
Read more >
Difference between node.js require and ES6 import and export
Node.js follows the commonJS module system, and it require to include modules that exist in separate files and for that purpose it has ......
Read more >
How To Use Node.js Modules with npm and package.json
When you first install a package to a Node.js project, npm automatically creates the node_modules folder to store the modules needed for your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found