Create a js script from typescript then import that script in a website project

My goal is to develop a javascript script using typescript, and finally import that script as a third party script in any website project (I tested with Nextjs so far).

My typescript project

Inventory

Here is my tsconfig.json file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"compilerOptions": {
"target": "ES2022",
"module": "UMD",
"moduleResolution": "Node",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}
</code>
<code>{ "compilerOptions": { "target": "ES2022", "module": "UMD", "moduleResolution": "Node", "outDir": "./dist", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true }, "include": ["src/**/*"] } </code>
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "UMD",
    "moduleResolution": "Node",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*"]
}

My package.json file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"name": "my-project-name",
"version": "0.0.1",
"description": "...",
"main": "index.js",
"scripts": {
"dev": "tsnd --respawn src/index.ts",
"build": "tsc -p tsconfig.json"
},
"repository": {
"type": "git",
"url": "..."
},
"keywords": [
"..."
],
"author": "...",
"license": "ISC",
"bugs": {
"url": "..."
},
"homepage": "...",
"devDependencies": {
"@types/node": "^20.12.8",
"typescript": "^5.4.5"
},
"dependencies": {
"ts-node-dev": "^2.0.0"
}
}
</code>
<code>{ "name": "my-project-name", "version": "0.0.1", "description": "...", "main": "index.js", "scripts": { "dev": "tsnd --respawn src/index.ts", "build": "tsc -p tsconfig.json" }, "repository": { "type": "git", "url": "..." }, "keywords": [ "..." ], "author": "...", "license": "ISC", "bugs": { "url": "..." }, "homepage": "...", "devDependencies": { "@types/node": "^20.12.8", "typescript": "^5.4.5" }, "dependencies": { "ts-node-dev": "^2.0.0" } } </code>
{
  "name": "my-project-name",
  "version": "0.0.1",
  "description": "...",
  "main": "index.js",
  "scripts": {
    "dev": "tsnd --respawn src/index.ts",
    "build": "tsc -p tsconfig.json"
  },
  "repository": {
    "type": "git",
    "url": "..."
  },
  "keywords": [
    "..."
  ],
  "author": "...",
  "license": "ISC",
  "bugs": {
    "url": "..."
  },
  "homepage": "...",
  "devDependencies": {
    "@types/node": "^20.12.8",
    "typescript": "^5.4.5"
  },
  "dependencies": {
    "ts-node-dev": "^2.0.0"
  }
}

I have a src/utils/Helpers.ts file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const Log = (what: any) => {
console.log(what)
}
</code>
<code>export const Log = (what: any) => { console.log(what) } </code>
export const Log = (what: any) => {
  console.log(what)
}

And finally a src/index.ts file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Log } from './utils/Helpers'
Log('Hello :) from Log')
console.log('Hello :) from console.log')
</code>
<code>import { Log } from './utils/Helpers' Log('Hello :) from Log') console.log('Hello :) from console.log') </code>
import { Log } from './utils/Helpers'

Log('Hello :) from Log')
console.log('Hello :) from console.log')

Compiling

Running the npm run dev command shows both expected console.log:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>> tsnd --respawn src/index.ts
[INFO] 15:02:17 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.2, typescript ver. 5.4.5)
Hello :) from Log
Hello :) from console.log
</code>
<code>> tsnd --respawn src/index.ts [INFO] 15:02:17 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.2, typescript ver. 5.4.5) Hello :) from Log Hello :) from console.log </code>
> tsnd --respawn src/index.ts

[INFO] 15:02:17 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.2, typescript ver. 5.4.5)
Hello :) from Log
Hello :) from console.log

Running the npm run build command generates a dist folder containing my .js files.

Running the node dist command shows both expected console.log, same result as npm run dev.

Finally, I put the whole thing on github and hosted it on netlify.


Third party script

I created a new Nextjs project and tried to import my script from netlify as a third party script:

In layout

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<script src="https://my-project-name.netlify.app/dist/index.js"></script>
<body className={inter.variable}>{children}</body>
</html>
)
}
</code>
<code>export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode }>) { return ( <html lang="en"> <script src="https://my-project-name.netlify.app/dist/index.js"></script> <body className={inter.variable}>{children}</body> </html> ) } </code>
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode
}>) {
  return (
    <html lang="en">
      <script src="https://my-project-name.netlify.app/dist/index.js"></script>
      <body className={inter.variable}>{children}</body>
    </html>
  )
}

With next component

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<Script src="https://my-project-name.netlify.app/dist/index.js" />
<body className={inter.variable}>{children}</body>
</html>
)
}
</code>
<code>export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode }>) { return ( <html lang="en"> <Script src="https://my-project-name.netlify.app/dist/index.js" /> <body className={inter.variable}>{children}</body> </html> ) } </code>
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode
}>) {
  return (
    <html lang="en">
      <Script src="https://my-project-name.netlify.app/dist/index.js" />
      <body className={inter.variable}>{children}</body>
    </html>
  )
}

In page component

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>'use client'
import Script from 'next/script'
export default function Home() {
return (
<Script
src="https://my-project-name.netlify.app/dist/index.js"
onLoad={() => {
console.log('Script has loaded')
}}
/>
)
}
</code>
<code>'use client' import Script from 'next/script' export default function Home() { return ( <Script src="https://my-project-name.netlify.app/dist/index.js" onLoad={() => { console.log('Script has loaded') }} /> ) } </code>
'use client'

import Script from 'next/script'

export default function Home() {
  return (
    <Script
      src="https://my-project-name.netlify.app/dist/index.js"
      onLoad={() => {
        console.log('Script has loaded')
      }}
    />
  )
}

Result

Neither script console.log is displayed, except the one saying ‘Script has loaded’, which makes me think that the script has been loaded, but for some reason, is not executed.

Plus, the call to index.js hosted on netlify is visible in the network tab in developer tools.

Can anyone help me on that one please ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật