[Question] Possible to import ts modules from node_modules?
See original GitHub issueHi,
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:
- Created a year ago
- Reactions:4
- Comments:9 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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).